Re: How to wait for async Future function to finish before building widget in scaffold?

1,038 views
Skip to first unread message
Message has been deleted

Suzuki Tomohiro

unread,
Jul 21, 2020, 8:14:20 PM7/21/20
to Faraz Khan, Flutter Development (flutter-dev)
the document path does not update in time and widget build fails as it does not get the path in time

But that shouldn’t happen because you’re using await for AndropidPathProvider.documentPath. The gotPath is only updated after _documentPath is updated. Would you check any exception?

Or are there other cases than initializaton that updates the gotPath variable?


On Tue, Jul 21, 2020 at 19:08 Faraz Khan <fara...@gmail.com> wrote:
Hi,

My main.dart is using android_path_provider package to get the documents path. This path is then passed to another class (stateful widget) which reads files from this path. 

The problem is that sometimes, the document path does not update in time and widget build fails as it does not get the path in time. I am calling the path getting in my ``initState`` and using ``whenComplete()`` to wait for it. But widget build still continues without waiting for this path to update.

This is my code, (relevant parts) which is achieving this. What am I doing wrong?

class EasyOCRMeat extends StatefulWidget {

@override
_EasyOCRMeatState createState() => _EasyOCRMeatState();
}

class _EasyOCRMeatState extends State<EasyOCRMeat> {


String _documentsPath = 'Unknown';
bool gotPath = false;

@override
void initState() {
super.initState();
//when the Future completes, set the bool flag to indicate this
initAndroidPaths().whenComplete(() {
setState(() {
gotPath = true;
});
});
}


// to get the downloads and documents paths of Android
Future<void> initAndroidPaths() async {
String documentsPath;
try {
documentsPath = await AndroidPathProvider.documentsPath;
} on PlatformException {}
if (!mounted) return;
setState(() {
_documentsPath = p.join(documentsPath, 'easyOCR');
});
}
@override
Widget build(BuildContext context) {
if (!gotPath){
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
}
return SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('App Lister'),
backgroundColor: Colors.teal,
),
body: HomeListView(
folderPath: _documentsPath,
),



I get the error:

I/flutter (14769): Another exception was thrown: FileSystemException: Directory listing failed, path = 'Unknown/' (OS Error: No such file or directory, errno = 2)

which shows that the path is not updated from the starting default value..

Am I doing this wrong? Is there a better way to go about this?


Thanks

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/7156e923-3d7d-45e9-9a47-0a91226830aco%40googlegroups.com.

Faraz Khan

unread,
Jul 21, 2020, 8:48:21 PM7/21/20
to Flutter Development (flutter-dev)
Thank you for reply.

I had to restart my android studio, and re-run from beginning and now its working.

Hopefully it continues to work in production also..

Thanks
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages