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