首页 > 解决方案 > 如何访问 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

我想要的只是将所有用户名放入一个列表中。

标签: fluttergoogle-cloud-firestore

解决方案


首先,我们获取所有用户名,然后将其传递给 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

推荐阅读