Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

firebase - Reload FirebaseAuth in Flutter

i have created in Flutter the following Code:

if (_auth.currentUser != null) {
       &&  FirebaseAuth.instance.currentUser.reload() != null
      Timer(
        Duration(seconds: 3),
        () => Navigator.of(context).pushAndRemoveUntil(
            MaterialPageRoute(
                builder: (context) =>
                    HomeScreen(username: _auth.currentUser.displayName)),
            (Route<dynamic> route) => false),
      );
    } else {
      Timer(Duration(seconds: 4),
          () => Navigator.pushReplacementNamed(context, "/auth"));
    }
  }

The Problem is actually that the Part && FirebaseAuth.instance.currentUser.reload() != null is not working. Do you now why? I want to reaload the currentUser-Firebase-AuthState every time the App is openend.

Thanks for helping!!!

question from:https://stackoverflow.com/questions/65859037/reload-firebaseauth-in-flutter

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reload() method is asynchronous and returns a Future<void>. You should use the await keyword to wait for a future to complete, see here.

It is not 100% clear to me why you need to use the reload() method. Since you use the displayName property in your code, is it because , in your app, this property is frequently changing? I would kindly suggest that you read in this SO answer the explanations on why one should use this method: "Calling reload() reloads that user's profile data from the server".

So, if it appears that you really need to use this method, you could do something along the following lines:

void navigate() async {

   if (_auth.currentUser != null) {

         await _auth.currentUser.reload();
         // continue your business logic here

         // For example
         if (_auth.currentUser.isEmailVerified) {
            // Navigate to ...
         }

   } else {...}

}

Note that, as explained in the FlutterFire doc, you could alternatively use the userChanges() method: "This stream provides realtime updates to the User class without having to call reload(), such as when credentials are linked, unlinked and when the user's profile is updated".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...