Navigator change page on async button press

Issue

Very new to Flutter. How would I push a new route predicated on a condition invoked by a button press? (asynchronous sign in call).

This sort of thing: if signIn (async) = success -> Navigator.push

Cheers

Widget buildButtons() {
    return new Container(
      child: new Column(
        children: <Widget>[
          new MaterialButton(
            minWidth: MediaQuery.of(context).size.width - 30, //FULL WIDTH - 30
            color: Style.palette3,
            padding: EdgeInsets.all(Style.padding1),
            child: new Text('Sign in',
                style: Style.signInBtn
            ),
            onPressed: () {
              if (LoginControl.signIn()) Navigator.push(context, MaterialPageRoute(builder: (context) => ArmDisarm()));
            },
          ),
          new FlatButton(
            child: Padding(
              padding: Style.paddingCreateAcc,
              child: new Text(
                  'Create an account',
                  style: Style.fontSize1
              ),
            ),
          ),
        ],
      ),
    );
  }

Solution

It’s better to show us the code of LoginControl.signIn().

Anyway if signIn() is a Future, then you to use async await

...
onPressed: () async {
  if (await LoginControl.signIn()) {
    Navigator.push(context, MaterialPageRoute(builder: (context) => ArmDisarm()));
  }
},
...

Make sure that signIn() method returns true if the user is successfully signed in.

Answered By – HasanAlyazidi

Answer Checked By – David Marino (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *