我想判断在主界面中启动哪个页面(实际上是登录页面和主页).所以我必须在首选项中阅读isLogin.主要怎么做?
我把这些代码Bundle 在一起:
Future<Null> checkIsLogin() async {
String _token = "";
// If token exist already, then HomePage
SharedPreferences prefs = await SharedPreferences.getInstance();
_token = prefs.getString("token");
print('get token from prefs: ' + _token);
if (_token != "" && _token != null) {
// already login
print("alreay login.");
isLogin = true;
}
}
void main() {
App.init();
// if we have token then go to HomePage directly otherwise go to LoginPage.
Widget _defaultHome = new LoginPage();
checkIsLogin();
if (isLogin) {
_defaultHome = new HomePage();
}
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
theme: globalThemeData,
home: _defaultHome
));
}
在上面的代码中,isLogin是一个全局变量.出现错误:
Performing full restart...
Restarted app in 2,810ms.
[VERBOSE-2:dart_error.cc(16)] Unhandled exception:
Invalid argument(s)
#0 _StringBase.+ (dart:core/runtime/libstring_patch.dart:245:57)
#1 checkIsLogin (file:///Volumes/xs/awesome/uranus/clients/flutter/flutter_asgard/lib/main.dart:17:34)
<asynchronous suspension>
#2 main (file:///Volumes/xs/awesome/uranus/clients/flutter/flutter_asgard/lib/main.dart:29:3)
#3 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:279:19)
#4 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)
似乎主要有调用异步的问题,如何让它工作?