class FoldersProvider extends ChangeNotifier {
final _folders = <Directory>[];
FoldersProvider() {
_addDefaultFoldersIfAccessible();
}
// more code
}
_addDefaultFoldersIfAccessible
is an async function.
I normally should await the Future<void>
result
but I can’t make the constructor async and I should still be able to instantiate it like a regular ChangeNotifier so it can still work as intended in my Flutter App.
Do you have any solution?
2
Answers
You can initialize the result value of
_addDefaultFoldersIfAccessible
as a "warm-up data" before running your app. To allow smooth transition, you can userunApp
twice in yourmain
function so that you can show a widget (perhaps a loading screen) before your main app is running.Ideally,
yourInitializingFunction
should handle all the necessary async tasks that are required by theChangeNotifier
so that you don’t need to make_addDefaultFoldersIfAccessible
async anymore.See also:
Since you can’t apply async to the body of a constructor you could try the following:
If you replace someAction with what you want to do when _addDefaultFoldersIfAccessible is complete. This value can be null.