この記事は公開から1年以上経過しています。
FlutterでFirebase.initializeApp()
を行ったとき
FlutterError (Binding has not yet been initialized.
The "instance" getter on the ServicesBinding binding mixin is only available once that binding has been initialized.
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.
In a test, one can call "TestWidgetsFlutterBinding.ensureInitialized()" as the first line in the test’s "main()" method to initialize the binding.
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.)
のようなエラーが発生したときの解決策。
原因
Flutterアプリケーションでバインディングが初期化されていない。
対応
Firebaseの初期化を行う前にWidgetsFlutterBinding.ensureInitialized()
でバインディングの初期化を行う。
void main() async {
WidgetsFlutterBinding.ensureInitialized(); // この行を追加
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const App());
}
参考ウェブサイトなど
- Flutter API Docs
ensureInitialized static method
以上です。