[英]What is the best way to check if the user is new in firebase auth with flutter
I have been using userCredential.additionalUserInfo!.isNewUser
to check if the user is new but this method has a huge downside which i will explain.我一直在使用
userCredential.additionalUserInfo!.isNewUser
来检查用户是否是新用户,但这种方法有一个巨大的缺点,我将对此进行解释。 In my app, I want to check if the user is new then if yes I will direct him to profile info page otherwise, he will be directed to different page.在我的应用程序中,我想检查用户是否是新用户,如果是,我会将他引导到个人资料信息页面,否则,他将被引导到不同的页面。 with this method (isNewUser), the first time the user sign in, it will work fine but if the user sign in for the first time then for some reason he close the app or signout before submitting the profile page, the next time he sign in he will not be treated as new user and he will not be directed to the profile page.
使用这种方法(isNewUser),用户第一次登录时,它会正常工作,但如果用户第一次登录,那么由于某种原因,他在提交个人资料页面之前关闭了应用程序或注销,下次他登录时因为他不会被视为新用户,也不会被定向到个人资料页面。
I came up with a way that does work but it also has its downside.我想出了一个可行的方法,但它也有它的缺点。
bool isExictedUser=false;
@override
void initState(){
super.initState();
checkIfUserHasData();
}
Future<void> checkIfUserHasData () async { // to check if the user has submitted a profile form before by checking if he has a name stored in the DB
var data = await FirebaseFirestore.instance
.collection('users')
.doc(userID)
.collection('personalInfo').doc(documentID)
.get().then((value) {
setState(() {
name = value.get('name');
});
});
if (name != null){
setState((){
isExictedUser = true;
});
}else {
return;
}
}
Widget build(BuildContext context) => isExictedUser
? const HomeScreen()
:
WillPopScope(
onWillPop: _onBackPressed,
child: Scaffold(
The problem with my method is that it takes few seconds to finish so even if the user is not new he will first be directed to the profile page for like 2 seconds until it confirm that he has a name stored.我的方法的问题是它需要几秒钟才能完成,所以即使用户不是新用户,他也会首先被引导到个人资料页面大约 2 秒钟,直到它确认他已经存储了一个名称。
Is there a way to optimize the (additionalUserInfo!.isNewUser) method or an alternative to keep showing the profile page until the user submit the form?有没有办法优化 (additionalUserInfo!.isNewUser) 方法或在用户提交表单之前继续显示个人资料页面的替代方法?
I usually keep a users' list in Firebase storage and manage users there.我通常在 Firebase 存储中保留用户列表并在那里管理用户。 That may help you as well.
这也可能对您有所帮助。
You can use FutureBuilder widget:您可以使用 FutureBuilder 小部件:
class MyFutureWidget extends StatelessWidget{
@override
Widget build(BuildContext context){
return FutureBuilder<FirebaseUser>(
future: FirebaseAuth.instance.currentUser(),
builder: (BuildContext context, AsyncSnapshot<FirebaseUser> snapshot){
if (snapshot.hasData){
FirebaseUser user = snapshot.data; // this is your user instance
/// is because there is user already logged
return MainScreen();
}
/// other way there is no user logged.
return LoginScreen();
}
);
}
}
For more information you can refer Documentation , stackoverflow thread , Firebase auth .有关更多信息,您可以参考文档、 stackoverflow 线程、 Firebase 身份验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.