Firebase 参数类型';列表<;评论数据>';can';不能分配给参数类型';列表<;Widget>;
我正在尝试使用从firebase查询的数据构建listview。但是我有一个错误“参数类型”列表Firebase 参数类型';列表<;评论数据>';can';不能分配给参数类型';列表<;Widget>;,firebase,flutter,flutter-listview,Firebase,Flutter,Flutter Listview,我正在尝试使用从firebase查询的数据构建listview。但是我有一个错误“参数类型”列表“无法分配给参数类型”列表“代码如下 Widget buildComments() { if (this.didFetchComments == false) { return FutureBuilder<List<CommentData>>( future: commentService.getComments(),
Widget buildComments() {
if (this.didFetchComments == false) {
return FutureBuilder<List<CommentData>>(
future: commentService.getComments(),
builder: (context, snapshot) {
if (!snapshot.hasData)
return Container(
alignment: FractionalOffset.center,
child: CircularProgressIndicator());
this.didFetchComments = true;
this.fetchedComments = snapshot.data;
return ListView(
children: snapshot.data, // where i'm having error
);
});
} else {
return ListView(children: this.fetchedComments);
}
}
Widget buildComments(){
if(this.didFetchComments==false){
回归未来建设者(
future:commentService.getComments(),
生成器:(上下文,快照){
如果(!snapshot.hasData)
返回容器(
对齐:分馏loffset.center,
child:CircularProgressIndicator());
this.didFetchComments=true;
this.fetchedComments=snapshot.data;
返回列表视图(
子项:snapshot.data,//发生错误的地方
);
});
}否则{
返回ListView(子项:this.fetchedComments);
}
}
我如何解决这个问题。错误本身就说明了问题
无法将参数类型“List
”分配给参数类型“List
”
如果要创建列表文本小部件以显示注释,可以使用
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) => Text(snapshot.data[index].*), //What ever you want to show in from your model
);
snapshot.data
返回List
,而ListView的子项需要一个小部件列表,因此会出现该错误
试着改变
return ListView(
children: snapshot.data,
);
例如:
return ListView(
children: Text(snapshot.data[index].userName), //change userName to whatever field of CommentData class you want to show
);
我建议使用ListView.Builder
来处理列表和索引。ListView
需要一个列表
,但您正在传递列表
您可以将列表视图修改为以下内容以更正错误
ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index) {
return Text(snapshot.data[index]['key']); //Any widget you want to use.
},
);