Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.

4649 단어 Flutter난치병
flutter sdk 업그레이드에 발생한 예외:
E/flutter (17545): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (17545): 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.
E/flutter (17545): 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.
E/flutter (17545): #0      defaultBinaryMessenger. (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter (17545): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter (17545): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter (17545): #3      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter (17545): #4      MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:349:48)
E/flutter (17545): #5      SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:158:25)
E/flutter (17545): #6      SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:25:17)
E/flutter (17545): #7      getTheme (package:wanandroid_flutter/main.dart:41:50)
E/flutter (17545): #8      main (package:wanandroid_flutter/main.dart:32:26)
E/flutter (17545): #9      _runMainZoned.. (dart:ui/hooks.dart:239:25)
E/flutter (17545): #10     _rootRun (dart:async/zone.dart:1126:13)
E/flutter (17545): #11     _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (17545): #12     _runZoned (dart:async/zone.dart:1518:10)
E/flutter (17545): #13     runZoned (dart:async/zone.dart:1502:12)
E/flutter (17545): #14     _runMainZoned. (dart:ui/hooks.dart:231:5)
E/flutter (17545): #15     _startIsolate. (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (17545): #16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter (17545): 
Lost connection to device.

당황하지 말고 두 번째 줄을 다시 한 번 보세요.
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.   yechaoa

주의: runApp() 이전에 바이너리 파일에 접근하거나 플러그인을 초기화하려면 WidgetsFlutterBinding.ensureInitialized() 를 호출해야 합니다.
오케이, 그럼 그대로 하자.
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

그렇다면 WidgetsFlutterBinding.ensureInitialized(); 이 코드는 도대체 무엇을 하는 것일까. WidgetsFlutterBinding 글자의 의미는Widget과 Flutter가 귀속되어 원본을 거슬러 올라가 보자.
/// A concrete binding for applications based on the Widgets framework.
///
/// This is the glue that binds the framework to the Flutter engine.
class WidgetsFlutterBinding extends BindingBase with GestureBinding, ServicesBinding, SchedulerBinding, PaintingBinding, SemanticsBinding, RendererBinding, WidgetsBinding {

  /// Returns an instance of the [WidgetsBinding], creating and
  /// initializing it if necessary. If one is created, it will be a
  /// [WidgetsFlutterBinding]. If one was previously initialized, then
  /// it will at least implement [WidgetsBinding].
  ///
  /// You only need to call this method if you need the binding to be
  /// initialized before calling [runApp].
  ///
  /// In the `flutter_test` framework, [testWidgets] initializes the
  /// binding instance to a [TestWidgetsFlutterBinding], not a
  /// [WidgetsFlutterBinding].
  static WidgetsBinding ensureInitialized() {
    if (WidgetsBinding.instance == null)
      WidgetsFlutterBinding();
    return WidgetsBinding.instance;
  }
}

BindingBase에서 계승한 다음에 제스처 귀속, 서비스 귀속 등이 있습니다. 한눈에 보면 초기화 작업입니다. 그리고 중간에 주석이 있습니다.
You only need to call this method if you need the binding to be initialized before calling [runApp].
상세하게 번역하지 않았습니다. 대의는 필요할 때 호출하는 것입니다. 그러면 언제 필요합니까? 시작으로 돌아가서 바이너리 파일에 접근하거나 플러그인을 초기화할 때 runApp() 전에 호출해야 합니다WidgetsFlutterBinding.ensureInitialized().
ok, 끝.

좋은 웹페이지 즐겨찾기