[Solved] Flutter Unhandled Exception: Binding has not yet been initialized.

2023/01/31 14:54

Installing buildappoutputsflutter-apkapp.apk…
Debug service listening on ws://127.0.0.1:2235/iDvEHhmKi5E=/ws
Syncing files to device AOSP on IA Emulator…
E/flutter (11009): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Binding has not yet been initialized.
E/flutter (11009): The “instance” getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
E/flutter (11009): Typically, this is done by calling “WidgetsFlutterBinding.ensureInitialized()” or “runApp()” (the latter calls the former). Typically this call is done in the “void main()” method. The “ensureInitialized” method is idempotent; calling it multiple times is not harmful. After calling that method, the “instance” getter will return the binding.
E/flutter (11009): In a test, one can call “TestWidgetsFlutterBinding.ensureInitialized()” as the first line in the test’s “main()” method to initialize the binding.
E/flutter (11009): If ServicesBinding is a custom binding mixin, there must also be a custom binding class, like WidgetsFlutterBinding, but that mixes in the selected binding, and that is the class that must be constructed before using the “instance” getter.

 

 

网上搜到最多的办法都是在main函数中第一行添加如下函数。但是我已经添加过了。

Solution

void main() {
    
    WidgetsFlutterBinding.ensureInitialized(); //Add this line before runApp()
    runApp(const MyApp());
}

Leave a Reply

Back to top