首页 > 解决方案 > 参数类型'列表' 不能分配给参数类型 'List

问题描述

我正在尝试使用从 firebase 查询的数据构建列表视图。但是我有一个错误'参数类型'List <CommentData>'不能分配给参数类型'List<Widget>'代码如下

    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); 
    }
  }

我该如何解决这个问题..

标签: firebaseflutterflutter-listview

解决方案


ListView期望 aList<Widgets>但你正在通过List<CommentData>

您可以将您的修改ListView为以下内容以纠正错误。

ListView.builder(
  itemCount: snapshot.data.length,
  itemBuilder: (context, index) {
    return Text(snapshot.data[index]['key']); //Any widget you want to use.
    },

);

推荐阅读