1

I have a button on pressed method that does some async task. At the end of this task, I need to redraw the entire widget with new updated arguments. This is my Widget Class.

class UIMain extends StatefulWidget {
  final bool loginState;
  final String userID;
  UIMain({Key key, @required this.loginState, @required this.userID})
      : super(key: key);
  @override
  _UIMainState createState() => _UIMainState();
}

This is the onPressed in the state of this class. The button is located in the drawer.

    onPressed: () async {
         Navigator.pop(context); // this is being called to close the drawer
         singOut().then((value) {
         Navigator.pushReplacement(
         context,
         MaterialPageRoute(
         builder: (BuildContext context) =>
         UIMain(loginState: false, userID: null,)),
         );
    });
   }

Here is the async task I am performing.

Future singOut() async {
  await _auth.signOut();
  await _googleSignIn.signOut();
}

I am getting Duplicate GlobalKey detected in widget tree. error. However, the changes are reflected as to what I want.

Also from a different widget.

await googleSignIn().then((result) {
  if (result != null) {
     print(result);
     Navigator.pushReplacement(
     context,
     MaterialPageRoute(
       builder: (BuildContext context) => UIMain(
       userID: result,
       loginState: true,
     )),
    );

This throws all kind of errors. The UI isn't rendered as it depends on result, which is getting passed as null. Here is the signInMethod.

Future<String> googleSignIn() async {
  if (await _googleSignIn.isSignedIn()) {
    _googleSignIn.signInSilently();
  } else {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;
    // get the credentials to (access / id token)
    // to sign in via Firebase Authentication
    final AuthCredential credential = GoogleAuthProvider.getCredential(
        accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
    _auth.signInWithCredential(credential);
  }

  return _googleSignIn.currentUser.id;
}

I don't think Navigator.pushReplacement is a better way of doing things. Also I understand that these async task takes time. How can I be sure that I get a non null result before redrawing the UIMain() with new parameters. And what other method should I employ to redraw instead of this Navigator.pushReplacement().

GunJack
  • 1,928
  • 2
  • 22
  • 35

1 Answers1

0

Make the Widget a StatefulWidget and call the setState method to update the GUI.

camillo777
  • 2,177
  • 1
  • 20
  • 20