首页 > 解决方案 > 返回地图时 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;
 }

我有这个代码的解决方法,但我需要了解为什么会这样以及发生了什么

标签: flutterdartfuture

解决方案


问题不在 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>'


推荐阅读