如何在Flutter中从Firebase获取当前用户ID [英] How to get the current user id from Firebase in Flutter
问题描述
我正在尝试-
final Future<FirebaseUser> user = auth.currentUser();
但问题在于,不是通过 userid制作文档,而是通过的名称-
but the problem is that instead of making a document by the "userid" it is making a document by the name of -
Instance of 'Future<FirebaseUser>'
从字面上看,这是我现在的文档名称,但是我想专门使其成为用户标识。
This is literally my documents name right now, but I want to make it the userid specifically.
什么我该怎么办?
推荐答案
更新(2020.09.09)
0.18.0版之后
firebase_auth 0.18.0中很少进行重大更新。 FirebaseUser现在称为User,currentUser是getter,currentUser是同步的。
Few breaking updates were made in firebase_auth 0.18.0. FirebaseUser is now called User, currentUser is a getter, and currentUser is synchronous.
这使得获取uid的代码如下:
This makes the code for getting uid like this:
final FirebaseAuth auth = FirebaseAuth.instance;
void inputData() {
final User user = auth.currentUser;
final uid = user.uid;
// here you write the codes to input the data into firestore
}
在0.18.0之前的版本
uid是FirebaseUser对象的属性。由于auth.currentUser()返回将来,因此必须等待才能获得如下用户对象:
uid is a property of FirebaseUser object. Since auth.currentUser() return a future, you have to await in order to get the user object like this:
final FirebaseAuth auth = FirebaseAuth.instance;
void inputData() async {
final FirebaseUser user = await auth.currentUser();
final uid = user.uid;
// here you write the codes to input the data into firestore
}
这篇关于如何在Flutter中从Firebase获取当前用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!