Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振共享首选项未正确返回布尔值_Flutter_Dart_Static_Boolean_Sharedpreferences - Fatal编程技术网

Flutter 颤振共享首选项未正确返回布尔值

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

我试图在用户第一次打开屏幕时才显示它。为此,我构建了helper函数:

 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){

}