首页 > 解决方案 > 如何通过 uid 从实例 user_type 获取数据(Firebase 颤振)

问题描述

如何通过 uid 从实例 user_type 获取数据(Firebase 颤振)

标签: firebaseflutter

解决方案


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

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

推荐阅读