Home > Software engineering >  cannot use navigator inside FutureBuilder
cannot use navigator inside FutureBuilder

Time:07-19

I wonder if i can use Navigator afther i get API data and I want to navigate to another page when i use ElevatedButton. But for some reason i cannot navigate to another page when i want to use it inside FutureBuilder.

This is the full code:

                     ElevatedButton(
                                    style: ElevatedButton.styleFrom(
                                      fixedSize: const Size.fromHeight(20),
                                      maximumSize: const Size.fromWidth(250),
                                      primary: clientSettings
                                          .getColor("login_button_color"),
                                      shape: const StadiumBorder(),
                                    ),
                                    onPressed: () {
                                      var credentials = {
                                        "data[email]": _emailController.text,
                                        "data[password]":
                                            _passwordController.text
                                      };
                                      FutureBuilder(
                                        future:
                                            DataFeed().getDataFeed(credentials),
                                        builder: (BuildContext context,
                                            AsyncSnapshot<dynamic> snapshot) {
                                          if (snapshot.hasData) {
                                            var dataFeed = snapshot.data;
                                            var _list = dataFeed["data"]
                                                    ["views"]
                                                .values
                                                .toList();
                                            Navigator.of(context)
                                                .pushAndRemoveUntil(
                                                    MaterialPageRoute(
                                                        builder: (context) =>
                                                            AppView(
                                                                selectedMenuItems:
                                                                    _list[0])),
                                                    (Route<dynamic> route) =>
                                                        false);
                                            return dataFeed;
                                          } else {
                                            return const CircularProgressIndicator();
                                          }
                                        },
                                      );
                                    },
                                    child: Text(
                                      'Login',
                                      style: TextStyle(
                                          fontFamily:
                                              clientSettings.getLogoAsset(
                                                  "login_font_family"),
                                          fontSize: clientSettings
                                              .getNumber("login_font_size"),
                                          color: clientSettings
                                              .getColor("login_text_color")),
                                    )),

CodePudding user response:

You can directly await an api call and navigate to the next page. You dont need a future builder

onPressed:() async{
 var response = await DataFeed().getDataFeed(credentials);
  Navigator.push();
}

Here reponse will have same data as snapshot.data.

  • Related