颤动中的共享首选项停止应用程序
Posted
技术标签:
【中文标题】颤动中的共享首选项停止应用程序【英文标题】:shared Preferences in flutter stop the app 【发布时间】:2021-05-17 16:59:05 【问题描述】:我在作为初始屏幕制作的页面中调用共享首选项
void initState()
super.initState();
getPreferences().then((value)
Timer(
Duration(seconds: 2),
(() =>
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => value))
),
);
);
这是我的方法
getPreferences() async
WidgetsFlutterBinding.ensureInitialized();//??**
SharedPreferences prefs = await SharedPreferences.getInstance();//the same with home screen in the skip button
bool loadingScreen=prefs.getBool('seen');
print("loadingScreen = $loadingScreen");
StatefulWidget _screen= LoginScr();
if (loadingScreen == null|| loadingScreen == false)
_screen = Tut();
else if (loadingScreen == true)
userName = prefs.getString('userName');
if(userName ==null||userName=="null")
print(userName+' userName in main');
_screen = LoginScr();
else
print(userName +'userName in main');
_screen = HomePage();
return _screen;
顺便说一句,我不知道这条线是什么
Widge,tsFlutterBinding.ensureInitialized();
//??**
这是我的日志
正在执行热重启... 将文件同步到构建的设备 android SDK 对于 x86... 在 1,776 毫秒内重新启动应用程序。我/颤振(8982): loadingScreen = true E/flutter (8982): [错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常: NoSuchMethodError:在 null 上调用了方法“+”。 E/颤振 ( 8982):接收者:null E/flutter(8982):尝试调用:+(“用户名 在 main") E/flutter (8982): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5) E/flutter (8982): #1 _SplashSCRState.getPreferences (package:plaster/UI/SplashSCR.dart:138:24) E/flutter (8982): E/颤振(8982):#2 _SplashSCRState.initState (package:plaster/UI/SplashSCR.dart:26:5) E/flutter (8982): #3 StatefulElement._firstBuild (包:flutter/src/widgets/framework.dart:4765:58) E/flutter ( 8982): #4 组件元素.mount (包:flutter/src/widgets/framework.dart:4601:5)E/flutter(8982): #5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #6 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #7 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #8 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #9 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #10 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #11 元素重建 (包:flutter/src/widgets/framework.dart:4343:5)E/flutter(8982): #12 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter (8982): #13 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5) E/flutter (8982): #14 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #15 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #16 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #17 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #18 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #19 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #20 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #21 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982):#22 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #23 StatefulElement.performRebuild (包:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #24 元素重建 (包:flutter/src/widgets/framework.dart:4343:5)E/flutter(8982): #25 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter (8982): #26 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4791:11) E/flutter ( 8982): #27 组件元素.mount (包:flutter/src/widgets/framework.dart:4601:5)E/flutter(8982): #28 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #29 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #30 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #31 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982):#32 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #33 SingleChildRenderObjectElement.mount (包:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982):#34 元素.inflateWidget (包:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #35 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982):#36 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982):#37 StatefulElement.performRebuild (包:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #38 元素重建 (包:flutter/src/widgets/framework.dart:4343:5)E/flutter(8982): #39 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4606:5) E/flutter (8982): #40 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4791:11) E/flutter ( 8982): #41 组件元素.mount (包:flutter/src/widgets/framework.dart:4601:5)E/flutter(8982): #42 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #43 元素.updateChild (包:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982):#44 ComponentElement.performRebuild (包:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #45 元素.r
【问题讨论】:
你能分享一下你的主要方法的全部内容吗 'prefs.getString('userName')' 返回 null。如果您正在检查 userName 是否为 null,那么您正在向其中添加一个字符串。 为了在@mahdishahbazi 的回答中添加一些内容,“ensureInitialized”创建并初始化了 Flutter 绑定。您可以在api.flutter.dev/flutter/flutter_test/TestWidgetsFlutterBinding/… 上找到更多信息 这里是主要的 void main() async runApp(MyApp()); 'prefs.getString('userName')' 返回 null。确保它在第一次应用程序运行时为空 【参考方案1】:您尝试打印日志:
print(userName + ' userName in main');
当userName
是null
:
if (userName == null || userName == "null")
它会引发异常。修改print
方法用法ant再试一次。应该是这样的:
userName = prefs.getString('userName');
if(userName == null || userName == "null")
print('first start, userName in main is null');
_screen = LoginScr();
else
print('$userName is userName in main');
_screen = HomePage();
【讨论】:
谢谢你,这是工作。这行 WidgetsFlutterBinding.ensureInitialized(); 怎么样 你可以删除它。问题在于访问包含null
的变量。
我无法删除您无法删除此问题,因为其他人已投入时间和精力来回答它。如需更多信息,请访问帮助中心。以上是关于颤动中的共享首选项停止应用程序的主要内容,如果未能解决你的问题,请参考以下文章
SharedPreferences 在 null 颤动上调用