【发布时间】:2019-12-02 01:52:56
【问题描述】:
我正在学习使用 FLUTTER 在 DART 中编程 但我处于一种我不明白的情况, 我正在使用这个函数来获取 SharedPreferences 数据
Iterable decoded = jsonDecode(data);
List<Item> result = decoded.map((x) => Item.fromJson(x)).toList();
但是如果我尝试发送为 null 将会出错! 所以我把这个如果
if(data != null)
但它甚至是 null 也通过了
我的代码
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
对不起我的英语不好
【问题讨论】:
-
尝试获取指定值并再次检查
标签: flutter dart sharedpreferences