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 如果(数据!=null)不为null,则忽略它_Flutter_Dart_Sharedpreferences - Fatal编程技术网

Flutter 如果(数据!=null)不为null,则忽略它

Flutter 如果(数据!=null)不为null,则忽略它,flutter,dart,sharedpreferences,Flutter,Dart,Sharedpreferences,我正在学习使用颤振在DART中编程 但我现在的处境我不明白, 我正在使用此函数获取SharedReferences数据 Iterable decoded = jsonDecode(data); List<Item> result = decoded.map((x) => Item.fromJson(x)).toList(); 但它甚至是空的 我的代码 Future load() async{ var prefs = await SharedPreferences.g

我正在学习使用颤振在DART中编程 但我现在的处境我不明白, 我正在使用此函数获取SharedReferences数据

Iterable decoded = jsonDecode(data);
List<Item> result = decoded.map((x) => Item.fromJson(x)).toList();
但它甚至是空的

我的代码

Future load() async{

    var prefs = await SharedPreferences.getInstance();
    var data = prefs.getString('data');

    // justing for testing
    print(data);

    if(data != null){

      // another test
      print("NOT IS NULL");
      Iterable decoded = jsonDecode(data);
      List<Item> result = decoded.map((x) => Item.fromJson(x)).toList();
      setState(() {
        widget.itens = result;
      });

    }else{
      print("IS NULL");
    }
  }
抱歉,我的英语不好

请尝试
如果(数据)
,因为该值可能未定义,在这种情况下,它是一个假值,但不等于null。

请注意,
打印(数据)
没有打印
null
;它打印了
[null]
(即带有方括号)


这意味着
data
不是
null
。它是一个包含单个
null
元素的
列表。

包含单个null元素的列表。这里的数据不是null。
尝试在SharedReference中添加新元素,然后再次测试。

看起来您的数据是一个数组,其中一个元素为null,但变量实际上不是null。estava olhando isso agora mesmo ele Retrona[null]não null,测试变量为任意值或空查询值,因此,您可以选择kkkk mas bom ja ajudou MUITO尝试获取指定值并再次检查
Future load() async{

    var prefs = await SharedPreferences.getInstance();
    var data = prefs.getString('data');

    // justing for testing
    print(data);

    if(data != null){

      // another test
      print("NOT IS NULL");
      Iterable decoded = jsonDecode(data);
      List<Item> result = decoded.map((x) => Item.fromJson(x)).toList();
      setState(() {
        widget.itens = result;
      });

    }else{
      print("IS NULL");
    }
  }
Restarted application in 3.464ms.
flutter: [null]
flutter: NOT IS NULL