Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase 参数类型';列表<;评论数据>';can';不能分配给参数类型';列表<;Widget>;_Firebase_Flutter_Flutter Listview - Fatal编程技术网

Firebase 参数类型';列表<;评论数据>';can';不能分配给参数类型';列表<;Widget>;

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(),

我正在尝试使用从firebase查询的数据构建listview。但是我有一个错误“参数类型”列表“无法分配给参数类型”列表“代码如下

    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.
    },

);