Flutter Firebase: How to refresh Firebase User object?

Viewed 344

Hi I'm working on flutter project for my study. I use Firebase to make a login system in my app. Below code is my RootPage in my app. I have problem in refreshing the user information. When there is no user displayName, then rootpage move to InputPage and I can input name. However, when I finish inputting name and updateDiplayName, it does not move to TapPage directly. I searched in stackoverflow and I add reload method but still not working. Can you give me some advice? By the way, last code is InputPage's trigger part.

 Widget build(BuildContext context) {
return StreamBuilder<User>(
    stream: FirebaseAuth.instance.authStateChanges(),
    builder: (context, snapshot){

      if(!snapshot.hasData){
        return LoginPage();
      }
      snapshot.data.reload();
      if(!snapshot.data.emailVerified){
        return EmailPage(snapshot.data);
      }
      if(snapshot.data.displayName==null){
        return InputPage(snapshot.data);
      }
      return TabPage(snapshot.data);

    }
);

}

onPressed: () {
              Navigator.pop(context); //this is alert dialog
              widget.user.updateDisplayName(_nameController.text);
             
              widget.user.reload();
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return TabPage(widget.user);
              }));

            },
1 Answers

If you check the documentation for User.reload() you'll see that it returns a Future<void>, so you need to await its results before the user is modified:

await widget.user.reload();

If you can't use await in the context where you need the user, you can use a FutureBuilder to accomplish the same.

Related