[英]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.