Flutter

[Flutter] Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized が発生した時の対処法

Flutterでときどきこんな実行時例外が発生する時の対処法です。

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. 
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 the `WidgetsFlutterBinding.ensureInitialized()` first. 
If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding. 
#0 defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:73:7) 
#1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:86:4) 
#2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62) 
#3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35) <asynchronous suspension> #4 MethodChannel.invokeMapMethod (package:f<…>

 

こんな簡単なことだとは。。。

対処法

mainメソッド内に以下の1行を追加するだけで解消します。

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // この1行を追加
  runApp(Delta(
    model: ProductDataModel(),
  ));
}

 

 

理由はよくわかりません。。。頭のいい方教えてください。

Flutterを学ぶなら世界最大級のオンライン学習サイトUdemyがおすすめ

Flutterを学ぶなら、世界最大級のオンライン学習サイトUdemy
がおすすめです。ああ、ここで上のことも学べばいいのか笑。


通常ですと、各コース2万円程度するのですが、年がら年中割引セールスを行なっており、割引後価格はなんとおおむね1500円程度で受講することができます。
今すぐ受講しなくても、安い時に大量に買っておいて、あとでじっくり勉強するのがかしこいやり方だと思います。

-Flutter

© 2025 かずのアプリときどきキャンプ飯 Powered by AFFINGER5