Flutter:将firestore快照转换为streambuilder中的列表
Posted
技术标签:
【中文标题】Flutter:将firestore快照转换为streambuilder中的列表【英文标题】:Flutter: Convert firestore snapshot to list in streambuilder 【发布时间】:2020-10-26 21:06:35 【问题描述】:我需要将快照从云存储库转换为列表,我知道这对于显示数据是不必要的,但我需要它根据其他参数重新排序数据,这是我的代码
Stream chatRooms;
List item = [];
Widget chatRoomsList()
return StreamBuilder(
stream: chatRooms,
builder: (context, snapshot)
if (snapshot.hasData &&
!snapshot.hasError)
item = [];
item = snapshot.data;
return ListView.builder(
itemCount: item.length,
shrinkWrap: true,
itemBuilder: (context, index)
return ChatRoomsTile(
otherUserUid:item[index]['arrayUsers']
.replaceAll("[", "")
.replaceAll(widget.user.uid, "")
.replaceAll("]", "")
.replaceAll(",", "")
.replaceAll(" ", ""),
chatRoomId:
item[index]["chatRoomId"],
user: widget.user,
);
);
else
return Container();
,
);
@override
void initState()
getUserInfogetChats();
super.initState();
getUserInfogetChats() async
DatabaseMethods().getUserChats(widget.user.uid).then((snapshots)
setState(()
chatRooms = snapshots;
);
);
我得到了这个错误
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following _TypeError was thrown building StreamBuilder<dynamic>(dirty, state: _StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#48820):
type 'QuerySnapshot' is not a subtype of type 'List<dynamic>'
【问题讨论】:
你在哪一行得到错误 @PeterHaddad 在这一行:item = snapshot.data; 【参考方案1】:变化:
item = snapshot.data;
进入这个:
item = snapshot.data.documents;
documents
应该返回一个List<DocumentSnapshot>
,所以还要更改item
的类型:
List<DocumentSnapshot> item = [];
【讨论】:
以上是关于Flutter:将firestore快照转换为streambuilder中的列表的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:Firebase 身份验证和 Firestore 快照流
如何使用flutter和firestore将子集合转换为列表?
可以从 Firestore 快照 Flutter 中获取元数据吗?