flutter 中的共享首选项停止应用程序
shared Preferences in flutter stop the app
我在作为初始屏幕制作的页面中调用共享首选项
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();
//??**
这是我的日志
Performing hot restart... Syncing files to device Android SDK built
for x86... Restarted application in 1,776ms. I/flutter ( 8982):
loadingScreen = true E/flutter ( 8982):
[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception:
NoSuchMethodError: The method '+' was called on null. E/flutter (
8982): Receiver: null E/flutter ( 8982): Tried calling: +(" userName
in 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/flutter ( 8982): #2
_SplashSCRState.initState (package:plaster/UI/SplashSCR.dart:26:5) E/flutter ( 8982): #3 StatefulElement._firstBuild
(package:flutter/src/widgets/framework.dart:4765:58) E/flutter (
8982): #4 ComponentElement.mount
(package: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 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #7 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8982): #8 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8982): #9 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #10 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8982): #11 Element.rebuild
(package: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 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #16 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8982): #17 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8982): #18 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #19 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8982): #20 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8982): #21 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #22 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8982): #23 StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4800:11) E/flutter (
8982): #24 Element.rebuild
(package: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 ComponentElement.mount
(package: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 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #30 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8982): #31 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8982): #32 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #33 SingleChildRenderObjectElement.mount
(package:flutter/src/widgets/framework.dart:6118:14) E/flutter (
8982): #34 Element.inflateWidget
(package:flutter/src/widgets/framework.dart:3569:14) E/flutter (
8982): #35 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #36 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8982): #37 StatefulElement.performRebuild
(package:flutter/src/widgets/framework.dart:4800:11) E/flutter (
8982): #38 Element.rebuild
(package: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 ComponentElement.mount
(package: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 Element.updateChild
(package:flutter/src/widgets/framework.dart:3327:18) E/flutter (
8982): #44 ComponentElement.performRebuild
(package:flutter/src/widgets/framework.dart:4652:16) E/flutter (
8982): #45 Element.r
您正在尝试打印日志:
print(userName + ' userName in main');
当userName
为null
时:
if (userName == null || userName == "null")
它引发异常。修改 print
方法用法再试一次。应该是这样的:
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();
}
我在作为初始屏幕制作的页面中调用共享首选项
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();
//??**
这是我的日志
Performing hot restart... Syncing files to device Android SDK built for x86... Restarted application in 1,776ms. I/flutter ( 8982): loadingScreen = true E/flutter ( 8982): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method '+' was called on null. E/flutter ( 8982): Receiver: null E/flutter ( 8982): Tried calling: +(" userName in 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/flutter ( 8982): #2
_SplashSCRState.initState (package:plaster/UI/SplashSCR.dart:26:5) E/flutter ( 8982): #3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58) E/flutter ( 8982): #4 ComponentElement.mount (package: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 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #7 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #10 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #11 Element.rebuild (package: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 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #16 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #22 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #23 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #24 Element.rebuild (package: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 ComponentElement.mount (package: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 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #30 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #33 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6118:14) E/flutter ( 8982): #34 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14) E/flutter ( 8982): #35 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #36 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #37 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4800:11) E/flutter ( 8982): #38 Element.rebuild (package: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 ComponentElement.mount (package: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 Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18) E/flutter ( 8982): #44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16) E/flutter ( 8982): #45 Element.r
您正在尝试打印日志:
print(userName + ' userName in main');
当userName
为null
时:
if (userName == null || userName == "null")
它引发异常。修改 print
方法用法再试一次。应该是这样的:
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();
}