简体   繁体   English

检查用户是否是firebase auth中的新用户的最佳方法是什么?

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

相关问题 在 firebase 中检查用户是否为新用户的最佳方法是什么 谷歌使用 flutter 登录 - What is the best way to check if the user is new in firebase google sign in with flutter 有什么方法可以检查用户是否是 flutter firebase 的新用户(Facebook 用户)? - Is there any way to check if a user is new in flutter firebase (A facebook user)? 检查用户是否登录 Flutter &amp; firebase auth | - Check if the user is logged in Flutter & firebase auth | Flutter 中的 Firebase 身份验证检查用户是否是新用户 - Firebase Authentication in Flutter Check if user is a new user 在 Flutter 中使用 Firebase 身份验证检查用户是否是新用户 - Check if user is new using Firebase Authentication in Flutter Flutter 中的 Firebase 身份验证状态检查 - Firebase Auth state check in Flutter 持久化用户Auth Flutter Firebase - Persist user Auth Flutter Firebase 将数据从 Firebase 存储到 SQLite 或在 flutter 中离线存储数据的最佳方法是什么? - What is the best way to store data from Firebase to SQLite or offline in flutter? 什么是每24小时自动更新Firebase的最佳方法是什么? - flutter what is the best way to update Firebase every 24 hours automatically? 将firebase_storage包含在flutter应用程序中的最佳方法是什么? - What is the best way to include firebase_storage in a flutter app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM