This problem is introduced when you upgrade Flutter.
The reason behind this is you are waiting for some data or running an async
function inside main()
.
I was initialising ScopedModel
inside main()
and inside that I was awaiting for some data.
There is a very small fix.
Just run WidgetsFlutterBinding.ensureInitialized()
inside void main()
, before you do runApp()
. Works like a charm!!
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(Delta(
model: ProductDataModel(),
));
}
This generally happens if you are awaiting on main()
method. So, the solution would be:
void main() {
// add this, and it should be the first line in main method
WidgetsFlutterBinding.ensureInitialized();
// rest of your app code
runApp(
MaterialApp(...),
);
}
Not sure if I have the correct answer, but I got the same error after a recent flutter upgrade, and managed to get it to work, so I'm sharing my findings.
Looks like the error might be caused by a recent breaking change: https://groups.google.com/forum/#!msg/flutter-announce/sHAL2fBtJ1Y/mGjrKH3dEwAJ.
As a result, we need to manually change the code as follows:
- If you're running an application and need to access the binary messenger before
runApp()
has been called (for example, during plugin initialization), then you need to explicitly call theWidgetsFlutterBinding.ensureInitialized()
first.- If you're running a test, you can call the
TestWidgetsFlutterBinding.ensureInitialized()
as the first line in your test'smain()
method to initialize the binding.
Alternatively, if you are a newbie like me and struggling to understand the above and #38464, you can temporarily avoid this problem by switching to the beta channel. Just run "flutter channel beta". The breaking change is not in beta channel yet, so after switching to beta channel you wouldn't get this error at least for now.
just add this line in main.dart
WidgetsFlutterBinding.ensureInitialized();
your code seems like
void main() {
WidgetsFlutterBinding.ensureInitialized();
return runApp(MultiProvider(
providers: [
ChangeNotifierProvider.value(
value: AppState(),
)
],
child: MyApp(),
));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With