简体   繁体   English

注册后如何获取currentUser的uid flutter firebase

[英]How to get uid of currentUser after sign up in flutter firebase

I want to get the uid and display it on screen after a user signs up.我想在用户注册后获取 uid 并将其显示在屏幕上。

FirebaseAuth.instance.auth.currentUser.uid;

After the user signs up the uid is null and I cant display the uid.用户注册后uid是null,我无法显示uid。 It works only when a user signs in but not when user signs up.它仅在用户登录时有效,但在用户注册时无效。 How can I show the uid just after user signs up?如何在用户注册后立即显示 uid?

EDIT:编辑:

This is the provider model这是提供者 model

class UserModel extends ChangeNotifier {
  final FirebaseAuth auth = FirebaseAuth.instance;

  getUid() {
    final User user = auth.currentUser;
    return user.uid;
  }
}

I am creating the user in a separate file (auth_helper.dart)我在一个单独的文件 (auth_helper.dart) 中创建用户

class AuthHelper {
  static FirebaseAuth _auth = FirebaseAuth.instance;

  static createAccountWithEmail({email, password}) async {
    final res = await _auth.createUserWithEmailAndPassword(
    email: email, password: password);
    final User user = res.user;

    return user;
  }
}

I want to save the User in my UserModel from inside the auth_helper.dart file above.我想从上面的 auth_helper.dart 文件中将用户保存在我的 UserModel 中。 How can I do so??我怎么能这样做?

You can get the signed up user's data after creating it.您可以在创建后获取注册用户的数据。 It will look like this:它看起来像这样:


  // Example code for registration.
  void _register() async {
    final User user = (await _auth.createUserWithEmailAndPassword(
      email: _emailController.text,
      password: _passwordController.text,
    ))
        .user;
    if (user != null) {
      setState(() {
        _success = true;
        _userEmail = user.email;
      });
    } else {
      _success = false;
    }
  }

You can check this official Github project of the firebase_auth package.您可以查看 firebase_auth package 的官方Github项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Firebase Web 持久性)登录后,currentUser 是 null,但是,在检查 auth object 后,它不是? - (Firebase Web Persistence) After sign in, currentUser is null, but, upon inspection of the auth object, it isn't? 如何在不请求的情况下在firebase-auth中首次加载时获取currentUser - How to get currentUser on first load in firebase-auth without requesting Flutter Riverpod Firebase currentUser Provider 未更新 - Flutter Riverpod Firebase currentUser Provider not updated 从 firebase 数据库 flutter 中的 uid 获取用户数据 - get user data from uid in firebase database flutter 如何使用 firebase_auth 包修复 flutter 应用程序中的 FirebaseAuth.instance.currentUser()? 它总是返回 null - How to fix FirebaseAuth.instance.currentUser() in flutter app using firebase_auth package? It is always returning null Flutter:如何将firebase uid设置为用户文档id? - Flutter: How to set firebase uid to users document id? 在 Flutter 中获取当前用户 uid 为 Firebase 的数据 - Fetch Firebase data with current user uid in Flutter Flutter - Firebase Auth UID 在 Firestore 中注册为 Null - Flutter - Firebase Auth UID registers as Null in Firestore Firebase 支付成功后才注册用户 - Firebase sign user up only after successful payment 在注册时存储用户名仅在页面刷新后显示 (Firebase) - Storing a username on sign up only displays after page refresh (Firebase)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM