flutter - 如何访问 Firestore 文档的元素
问题描述
我想获取用户名,该用户名是我之前保存在“用户”集合中的文档中的。在输入 typeaheadTextField 时,我希望用户能够从建议的用户列表中进行选择。为此,我不知道如何获取用户名,这可能是对用户文本输入的适当建议。
_firebaseFirestore
.collection('user')
.where('username', isGreaterThanOrEqualTo: username)
.snapshots();
到目前为止,上面的代码基本上是我的想法。为了更好地理解我的问题,下面的树结构显示了我的 firestorm 项目中的结构
user(collection)
----User1(document1)
--user_id
--username
--age
----User2(document2)
--user_id
--username
--age
----User3(document3)
--user_id
--username
--age
我想要的只是将所有用户名放入一个列表中。
解决方案
首先,我们获取所有用户名,然后将其传递给 typeaheadTextField
QuerySnapshot<Map<String, dynamic>> users =
await _firebaseFirestore.collection('user').get();
List<String> usernames = users.docs.map((e) => e.get('username')).toList();
// usernames is your list of username. You can then pass that to the typeaheadTextField
推荐阅读
- java - java数组字节文件到人类可读
- typescript - 为什么我收到 NoExplicitTypeError?
- reactjs - 如何从 React 上下文中只导入我需要的函数
- c# - 如何检查数据库中是否存在特定字段,如果不存在则添加记录,否则进行编辑
- tensorflow - Keras `ImageDataGenerator` 图像和蒙版的增强方式不同
- python - Django - calling function does not redirect
- python - 如何将信息从 GUI 发送到 Excel 电子表格?下面是我的代码
- sql - 我可以使用 SQL 构建包含系列的 Access 图表吗?
- python-3.x - 如何为python构建一个Web应用程序>?
- php - 使 /storage 文件夹在 laravel 中公开可用