如何在 Flutter 中从 Firebase 获取当前用户 ID [英] How to get the current user id from Firebase in Flutter

查看:39
本文介绍了如何在 Flutter 中从 Firebase 获取当前用户 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个 -

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>'

这实际上是我现在的文档名称,但我想特别将其设为用户 ID.

This is literally my documents name right now, but I want to make it the userid specifically.

我该怎么办?

推荐答案

更新(2020.09.09)

firebase_auth 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
}

firebase_auth 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;

Future<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆