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

简介: Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.

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.<anonymous closure> (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.<anonymous closure>.<anonymous closure> (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.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (17545): #15     _startIsolate.<anonymous closure> (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() 。



ok,那就照着来吧


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,结束。



目录
相关文章
Flutter之运行提示Could not update files on device: Connection closed before full header was received
Flutter之运行提示Could not update files on device: Connection closed before full header was received
727 0
|
4月前
|
缓存 监控 前端开发
【Flutter 前端技术开发专栏】Flutter 应用的启动优化策略
【4月更文挑战第30天】本文探讨了Flutter应用启动优化策略,包括理解启动过程、资源加载优化、减少初始化工作、界面布局简化、异步初始化、预加载关键数据、性能监控分析以及案例和未来优化方向。通过这些方法,可以缩短启动时间,提升用户体验。使用Flutter DevTools等工具可助于识别和解决性能瓶颈,实现持续优化。
208 0
【Flutter 前端技术开发专栏】Flutter 应用的启动优化策略
|
3月前
|
开发框架 前端开发 测试技术
Flutter开发常见问题解答
Flutter开发常见问题解答
|
4月前
|
前端开发 C++ 容器
Flutter-完整开发实战详解(一、Dart-语言和-Flutter-基础)(1)
Flutter-完整开发实战详解(一、Dart-语言和-Flutter-基础)(1)
|
14天前
|
JSON Dart Java
flutter开发多端平台应用的探索
flutter开发多端平台应用的探索
26 6
|
14天前
|
JSON Dart Java
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
flutter开发多端平台应用的探索 下 (跨模块、跨语言通信之平台通道)
|
17天前
|
安全 Android开发 开发者
探索安卓开发的未来:Kotlin的崛起与Flutter的挑战
在移动开发的广阔天地中,安卓平台始终占据着举足轻重的地位。随着技术的不断进步和开发者需求的多样化,Kotlin和Flutter成为了改变游戏规则的新玩家。本文将深入探讨Kotlin如何以其现代化的特性赢得开发者的青睐,以及Flutter凭借跨平台的能力如何挑战传统的安卓开发模式。通过实际案例分析,我们将揭示这两种技术如何塑造未来的安卓应用开发。
43 6
|
1月前
|
开发框架 Android开发 iOS开发
Flutter相关痛点解决问题之淘特选择桌面端开发框架如何解决
Flutter相关痛点解决问题之淘特选择桌面端开发框架如何解决
|
1月前
|
移动开发 前端开发 JavaScript
"跨界大战!React Native、Weex、Flutter:三大混合开发王者正面交锋,揭秘谁才是你移动应用开发的终极利器?"
【8月更文挑战第12天】随着移动应用开发的需求日益增长,高效构建跨平台应用成为关键。React Native、Weex与Flutter作为主流混合开发框架各具特色。React Native依托Facebook的强大支持,以接近原生的性能和丰富的组件库著称;Weex由阿里巴巴开发,性能优越尤其在大数据处理上表现突出;Flutter则凭借Google的支持及独特的Dart语言和Skia渲染引擎,提供出色的定制能力和开发效率。选择时需考量项目特性、团队技能及生态系统的成熟度。希望本文对比能助你做出最佳决策。
118 1
|
1月前
|
Dart Android开发 iOS开发
Flutter相关痛点解决问题之提升开发效率如何解决
Flutter相关痛点解决问题之提升开发效率如何解决