Flutter 颤振共享首选项未正确返回布尔值
我试图在用户第一次打开屏幕时才显示它。为此,我构建了helper函数:Flutter 颤振共享首选项未正确返回布尔值,flutter,dart,static,boolean,sharedpreferences,Flutter,Dart,Static,Boolean,Sharedpreferences,我试图在用户第一次打开屏幕时才显示它。为此,我构建了helper函数: static Future<bool> measureCorrectlyWasNotShownBefore() async { SharedPreferences prefs = await SharedPreferences.getInstance(); bool _seen = prefs.getBool(_measureCorrectlyKey) ?? false; if (_s
static Future<bool> measureCorrectlyWasNotShownBefore() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = prefs.getBool(_measureCorrectlyKey) ?? false;
if (_seen) {
return true;
} else {
await prefs.setBool(_measureCorrectlyKey, true);
return false;
}
}
然而,弗利特抱怨:
条件必须具有静态类型“bool”。
试着改变条件
我怎样才能解决这个问题?我在这里遗漏了什么?您的
度量值在之前未正确显示返回的是未来
而不是bool
。因此,您不能在if
中给出它,其中if
需要bool
类型
您需要等待将来完成,然后使用if语句进行检查
final result =
await LocalStorageService.measureCorrectlyWasNotShownBefore();
if(result){
}
final result =
await LocalStorageService.measureCorrectlyWasNotShownBefore();
if(result){
}