본문 바로가기
Flutter/Flutter FAQ

flutter 플러터: 처리되지 않은 예외: 바인딩이 초기화되기 전에 ServicesBinding.defaultBinaryMessenger에 액세스했습니다., Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was..

by 베타코드 2023. 5. 9.
반응형

질문


이 문제를 해결하는 해결책이 있나요?

스택 트레이스:

[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<…>

답변


이 문제는 Flutter를 업그레이드 할 때 발생합니다. 그 이유는 main() 안에서 데이터를 기다리거나 async 함수를 실행하기 때문입니다.

저는 main() 안에서 ScopedModel을 초기화하고 그 안에서 데이터를 기다리고 있었습니다.

매우 간단한 해결책이 있습니다. runApp()을 실행하기 전에 void main() 안에서 WidgetsFlutterBinding.ensureInitialized()를 실행하면 됩니다. 매우 간단하죠!

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(Delta(
    model: ProductDataModel(),
  ));
}
반응형

댓글