Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
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, 끝.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Flutter】DateTime 전월의 일수를 취득한다달의 일수를 취득할 필요가 있어, 의외로 수요 있을까라고 생각했으므로 비망록 정도에 남겨 둡니다. DateTime 날짜에 0을 입력하면 전월 DateTime이 됩니다. 2021년 3월 0일 = 2021년 2월 28일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.