gpt4 book ai didi

list - Flutter:将 Firestore 快照转换为流构建器中的列表

转载 作者:行者123 更新时间:2023-12-03 02:59:56 25 4
gpt4 key购买 nike

我需要将快照从 cloud firestore 转换为列表,我知道这不需要显示数据,但我需要它根据其他参数重新排序数据,这是我的代码

 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>'

最佳答案

改变:

item = snapshot.data;
进入这个:
item = snapshot.data.documents;
documents应该返回 List<DocumentSnapshot> , 所以还要改变 item 的类型:
List<DocumentSnapshot> item = [];

关于list - Flutter:将 Firestore 快照转换为流构建器中的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62761174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com