我是flutter的新手,在如何使用语法获取当前用户文档ID/名称方面遇到了一些问题。
Future<User?> readUser() async {
final docUser =
FirebaseFirestore.instance.collection('users').doc('How do I get this document ID');
final snapshot = await docUser.get();
if (snapshot.exists) {
return User.fromJson(snapshot.data()!);
} else {
print('User does not exist');
}
}
这是我的User类,我在其中存储我的详细信息
class User {
String uid;
final String name;
final int age;
final String email;
User({
this.uid = '',
required this.name,
required this.age,
required this.email,
});
Map<String, dynamic> toJson() => {
'uid': uid,
'name': name,
'age': age,
'email': email,
};
static User fromJson(Map<String, dynamic> json) => User(
uid: json['id'],
name: json['name'],
age: json['age'],
email: json['email'],
);
}
我试过了
final firebaseUser = await FirebaseAuth.instance.currentUser
Future<User?> readUser() async {
final docUser =
FirebaseFirestore.instance.collection('users').doc(firebaseUser.uid);
final snapshot = await docUser.get();
if (snapshot.exists) {
return User.fromJson(snapshot.data()!);
} else {
print('User does not exist');
}
}
2条答案
按热度按时间s6fujrry1#
只是一个月
例如
kmb7vmvb2#
要访问当前用户ID,请使用**
FirebaseAuth.instance.currentUser!.uid
**要访问当前用户名,请使用**
FirebaseAuth.instance.currentUser!.displayName
**如何监听当前用户的文档?