flutter - 返回地图时 FutureBuilder 中的空快照
问题描述
我不知道为什么我在未来的构建器中总是得到空返回,而我确定应该有一个值
//global variables
Map<String,bool> students;
List<String> studentsList ;
List<bool> studentsCheck ;
FutureBuilder(
future: GetInfoFromBackEnd(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return snapshot.hasData?
CustomScrollView(....some not important widgets here....):CircularProgressIndicator();
},)
Future GetInfoFromBackEnd() async {
students = {
'this info should be taken from backend0':false,
'this info should be taken from backend0':false,
'this info should be taken from backend0':false,
'this info should be taken from backend0':false,
'this info should be taken from backend0':false,
};
studentsList = students.keys;
studentsCheck = students.values;
return students;
}
我有这个代码的解决方法,但我需要了解为什么会这样以及发生了什么
解决方案
问题不在 FutureBuilder 或地图中,实际上,当我试图获取 List 中的学生 Map 值和键时,它会破坏 Future 并阻止它返回正确的值,所以我在 snapshot.data 中得到了 null;这是我修复它的方法:
studentsList = students.keys.toList();
studentsCheck = students.values.toList();
return students;
PS:错误是type '_CompactIterable<String>' is not a subtype of type 'List<String>'
推荐阅读
- visual-studio-2012 - 从源代码管理中获取特定版本
- salesforce - 我是否需要安全令牌才能连接到 Salesforce API?
- javascript - 如何将值输入到 javascipt 的对象中?
- gerrit - 阻止直接推送到分支
- python - Linux:Python 无法下载 pip
- matlab - Matlab fwrite 精度转换
- build - bazel build:无法链接外部非 bazel 共享库
- c++ - 使用和不使用移动从 unique_ptr 构造 shared_ptr?
- regex - ruby - 如何使字母小写,除非它出现在逗号或正则表达式的斜线之后
- php - NetSuite PHP 为 InventoryBalance 类型保存了搜索调用