例如,如果我尝试获取数据snapshot.data.lenght
,它将无法正常工作(如果按Tab键甚至无法显示),并且出现以下错误:
在构建FutureBuilder>时引发了以下NoSuchMethodError(脏,状态:_FutureBuilderState>#7a7e2): 类“列表”没有实例获取器“文档”。 接收方:“ _ GrowableList”的实例(长度:2) 尝试致电:文档
我的代码:
Future<List> _getPlants() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("easy_plants").getDocuments();
List<PlantsMainItem> plantsmainnew = [];
for (int i = 0; i < qn.documents.length; i++) {
var a = qn.documents[i];
PlantsMainItem plantsitem = PlantsMainItem(
a.data["title"],
a.data["titlelatin"],
a.data["content"],
NetworkImage(a.data["image"]),
a.data["sunlevel"],
a.data["level"]);
print(plantsitem.titlelatin);
plantsmainnew.add(plantsitem);
}
//print(plantsmainnew.length);
return plantsmainnew;
}
小部件构建(BuildContext上下文){
return Container(
child: FutureBuilder(
future: _getPlants(),
builder: (context, snapshot) {
if(snapshot.data == ConnectionState.waiting) {
return Container(
height: 200,
width: 400,
child: Padding(
padding: EdgeInsets.all(20),
child: Center(
child: Container(
child: LinearProgressIndicator(),
),
),
),
);
} else {
print(snapshot.data);
return Text("TEST", style: TextStyle(fontSize: 30));
}
}
),
);
}
答案 0 :(得分:0)
该错误表明您的“列表”类没有文档获取程序,是否可以尝试从函数中删除“列表”的相同代码?
Future _getPlants() async {
var firestore = Firestore.instance;
QuerySnapshot qn = await firestore.collection("easy_plants").getDocuments();
List<PlantsMainItem> plantsmainnew = [];
for (int i = 0; i < qn.documents.length; i++) {
var a = qn.documents[i];
PlantsMainItem plantsitem = PlantsMainItem(
a.data["title"],
a.data["titlelatin"],
a.data["content"],
NetworkImage(a.data["image"]),
a.data["sunlevel"],
a.data["level"]);
print(plantsitem.titlelatin);
plantsmainnew.add(plantsitem);
}
我认为,如果您对FutureBuilder使用以下结构,那么建议只是更可靠:
if (snapshot.connectionState != ConnectionState.done)
return YOUR WAITING ACTION;
if (snapshot.connectionState == ConnectionState.done && snapshot.hasData)
if (snapshot.hasError) {YOUR ERROR SCREEN}
return YOUR ACTUAL SCREEN