所以我尝试了共享首选项让用户在他们注销之前仍然登录,但是当我尝试重新启动应用程序时会出现这种情况
[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
#0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36)
#2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12)
#3 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:358:49)
#4 MethodChannelSharedPreferencesStore.getAll (package:shared_preferences_platform_interface/method_channel_shared_preferences.dart:44:25)
#5 SharedPreferences._getSharedPreferencesMap (package:shared_preferences/shared_preferences.dart:180:57)
#6 SharedPreferences.getInstance (package:shared_preferences/shared_preferences.dart:56:19)
#7 read_loggedIn (package:indonesia_bisa_2/API/LocalService.dart:11:41)
#8 main (package:indonesia_bisa_2/Main/main.dart:16:14)
#9 _runMainZoned.<anonymous closure>.<…>
这是检查代码
import 'package:shared_preferences/shared_preferences.dart';
Future<void> save_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
prefs.setString("loggedIn", "true");
}
//this is for checking the saved variable to check if the user is already logged in or not
Future<String> read_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
var value = prefs.getString("loggedIn");
return value;
}
这是使用代码的时间
Future<void> main() async {
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: (read_loggedIn() == "true") ? new SPAL (): new MyApp()));
}
答案 0 :(得分:1)
问题是你调用了 read_loggedIn()
,它是一个未来,但你直接调用了它,虽然它没有返回值,但你得到了一个空值。所以你必须 await
直到你从 read_loggedIn()
得到结果,然后你才能使用它的值。
用下面的代码替换你的主要方法
Future<void> main() async {
// you have to wait untill get the result
var login = await read_loggedIn();
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
// and you must check if your login value is null or not
home: (login != null && login == "true") ? new SPAL(): new MyApp()));
}
答案 1 :(得分:1)
您可以像这样更改 main
方法:
import 'package:shared_preferences/shared_preferences.dart';
Future<void> save_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool("loggedIn", true);
}
Future<Bool> read_loggedIn() async {
final prefs = await SharedPreferences.getInstance();
var value = prefs.getBool("loggedIn");
return value;
}
Future<void> main() async {
final bool login = await read_loggedIn();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: (login ?? false) ? SPAL() : MyApp(),
),
);
}
答案 2 :(得分:1)
确保在WidgetsFlutterBinding.ensureInitialized();
之前调用await SharedPreferences.getInstance();
例如在您的 main.dart 中:
void main() async {
await runZonedGuarded(
() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
runApp(MyApp(prefs));
},
(error, st) => print(error),
);
}
?
答案 3 :(得分:0)
当我用 iphone 模拟器测试我的应用程序时,我得到了完全相同的错误。 (我有一个 m1 mac)。这可能是一个“时间问题”。 我在 SharedPreferences.getInstance 之前添加了这一行 await Future.delayed(Duration(seconds: 2)); 并且错误消失了。