Flutter 中列表视图构建器的快照长度 [已修复]
Posted
技术标签:
【中文标题】Flutter 中列表视图构建器的快照长度 [已修复]【英文标题】:Length of snapshot for List View builder in flutter[Fixed] 【发布时间】:2021-10-01 20:21:02 【问题描述】:我需要在 Flutter 上实现一个 ListView,并且我将 snapshot.data.length 作为 itemCount 的参数传递
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
return ListTile(
title: Text(
snapshot.data[index].data["Identificacao"],...
然后我得到一个错误:
I/flutter ( 4647): Class 'List<DocumentSnapshot>' has no instance getter 'length'.
I/flutter ( 4647): Receiver: Instance(length:1) of '_GrowableList'
I/flutter ( 4647): Tried calling: length
【问题讨论】:
【参考方案1】:很容易解决
solved the problem by replacing StreamBuilder<Object> with StreamBuilder<QuerySnapshot>. by
default the StreamBuilder comes in this form StreamBuilder<Object>
这是一个例子
StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance
.collection('chats/hgjgjgjGW/messages')
.snapshots(),
builder: (cnxt, snapshot)
if (snapshot.connectionState == ConnectionState.waiting)
return Center(
child: CircularProgressIndicator(),
);
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (ctx, i) => Container(
padding: EdgeInsets.all(10),
child: Text(snapshot.data!.docs[i]['text']),
),
);
return Center(
child: Text('error'),
);
),
【讨论】:
以上是关于Flutter 中列表视图构建器的快照长度 [已修复]的主要内容,如果未能解决你的问题,请参考以下文章
Flutter FutureBuilder 小部件动态列表长度
如何在已修改为不运行资源管理器的 Windows 系统上启动时运行应用程序