【问题标题】:How to get data from instance user_type by uid (Firebase flutter)如何通过 uid 从实例 user_type 获取数据(Firebase 颤振)
【发布时间】:2021-08-27 14:03:35
【问题描述】:

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: firebase flutter


【解决方案1】:

为您的用户集合创建一个变量

final CollectionReference _usersCollection =
  FirebaseFirestore.instance.collection("users");

根据userId值查询用户文档快照

final QuerySnapshot<Object?> usersQuery = await _usersCollection.where("uid",isEqualTo: userId).limit(1).get();

您可以从文档快照中获取地图数据

if(usersQuery.length>0) {
  final Map<String, dynamic> userData =
      usersQuery.docs[0].data() as Map<String, dynamic>;
  return userData['userType']; // you can use the data as you wish here am returning it
}
// else do something when the data isn't available

【讨论】:

  • 在数据库中,我有一个 userType 字段。总的来说,我想获取它的数据以便以后工作,但我无法制作方法 getter。
  • 我已经添加了答案和解释,如果有什么不清楚的地方可以问我。这些代码可能会因您使用的 firebase 版本而异,所以如果遇到困难,请告诉我。
  • 谢谢你的回答,我明天去看看
  • 当然,如果需要请告诉我
【解决方案2】:
final _firestoreInstance = FirebaseFirestore.instance;

// returns a list of user documents
final res = await _firestoreInstance.collection("user_type").get();
// returns the first user_type instance of the list
final firstUser = res.docs.first.data();

return firstUser['uid'];

【讨论】:

    猜你喜欢
    • 2019-11-01
    • 2022-10-06
    • 2021-02-16
    • 2021-10-15
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多