flutter - 编写此飞镖迭代并返回列表的更好方法是什么
问题描述
目的是进行某种连接。从 firestore 中的用户文档中获取每个用户文档,并通过 uid 将其附加到某个集合中。然后返回包含用户文档完整详细信息的集合,而不仅仅是 uid。
Future<List<ChatRoom>> getChatsrooms(uid) async {
ChatRoom chatroom = new ChatRoom();
try {
List<ChatRoom> chatrooms = new List();
var snapshots = await _db
.collection('my-chats-chats')
.where('users', arrayContains: uid)
.orderBy('updated_at', descending: true)
.get();
for (var room in snapshots.docs) {
chatroom.chatroomid = room.data()['chatroomid'];
chatroom.users = room.data()['users'];
// get user profile that isn'nt u.
List<dynamic> userids = await room.data()['users']
..remove(uid);
var doc = await _db.collection('my-chats-users').doc(userids[0]).get();
UserModel user = UserModel.fromMap(doc.data());
chatroom.users.add(user);
// Remove the users string UID from users list
chatroom.users.remove(user.uid);
chatroom.users.remove(uid);
chatrooms.add(chatroom);
}
return chatrooms.toList();
} catch (e) {
print("Couldn't get user\'s chatrooms exception: " + e.toString());
return null;
}
}
上面似乎做了我想要的,除了返回的聊天室列表,只包含一个聊天室的重复项。因此,即使有 10 个不同的聊天室,我也只会重复一个聊天室 10 次。
我在想的是chatrooms.add(chatroom);
唯一添加的一项。但是聊天室列表应该是一个可增长的列表。那么为什么最终只有一个重复的项目呢?
写这个更好的方法是什么?
解决方案
推荐阅读
- javafx-2 - 如何在javafx中加粗和更改tableview项目或行的字体大小?
- dns - 了解 Pingdom DNS 测试结果
- python - 'ToastNotifier' 对象没有属性 'classAtom'
- node.js - firestore nodejs解析错误:意外的令牌
- jquery - 重新加载页面后保持活动的 jQuery UI 选项卡和与其相关的子选项卡
- php - PHP Openssl_pkey_get_private() 结果取决于 openssl
- c# - 在 Linux 中,找不到框架“.NETFramework,Version=v4.5”的参考程序集
- php - 在多个堆叠特征中链接相同的方法
- javascript - jQuery:如何使进度条从一种颜色变为另一种颜色
- javascript - 自定义控件的 HTML 项 - cognos 11.0.7