gpt4 book ai didi

flutter - 错误 :flutter/lib/ui/ui_dart_state. cc(199) 未处理的异常:对空值使用空检查运算符

转载 作者:行者123 更新时间:2023-12-04 12:11:21 25 4
gpt4 key购买 nike

我正在尝试通过可注入(inject)生成,我做了 flutter clean 也行不通,我使用的是稳定版本的 flutter 2.2.2,我在这行代码上遇到了空检查错误
此代码抛出错误

SharedPreferences.getInstance();
还有这个代码
   @module
abstract class ThemeModule {
@preResolve
Future<SharedPreferences> get prefs async =>
await SharedPreferences.getInstance();
}
错误堆栈
E/flutter (27063): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (27063): #0 MethodChannel.binaryMessenger
package:flutter/…/services/platform_channel.dart:142
E/flutter (27063): #1 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:148
E/flutter (27063): #2 MethodChannel.invokeMethod
package:flutter/…/services/platform_channel.dart:331
E/flutter (27063): #3 MethodChannel.invokeMapMethod
package:flutter/…/services/platform_channel.dart:358
E/flutter (27063): #4 MethodChannelSharedPreferencesStore.getAll
package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:44
E/flutter (27063): #5 SharedPreferences._getSharedPreferencesMap
package:shared_preferences/shared_preferences.dart:180
E/flutter (27063): #6 SharedPreferences.getInstance
package:shared_preferences/shared_preferences.dart:56
E/flutter (27063): #7 main
package:financial/main.dart:12
E/flutter (27063): #8 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter (27063): #9 _rootRun (dart:async/zone.dart:1354:13)
E/flutter (27063): #10 _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter (27063): #11 _runZoned (dart:async/zone.dart:1789:10)
E/flutter (27063): #12 runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter (27063): #13 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter (27063): #14 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter (27063): #15 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

最佳答案

我有同样的问题,解决方案是添加 WidgetsFlutterBinding.ensureInitialized()在运行应用程序之前的主要功能中:

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// init your dependency injection here
runApp(MyApp());}
来自 docs :
返回 WidgetsBinding 的一个实例,必要时创建并初始化它。如果创建了一个,
它将是一个 WidgetsFlutterBinding。如果之前已初始化,那么它至少会实现 WidgetsBinding。
如果您需要在调用 runApp 之前初始化绑定(bind),则只需调用此方法。

关于flutter - 错误 :flutter/lib/ui/ui_dart_state. cc(199) 未处理的异常:对空值使用空检查运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67955075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com