简体   繁体   中英

My "If" statement doesn't work in dart/flutter

I have an if statement in futureBuilder and it doesn't return something. Plus that my futureBuilder it doesn't have model and you shouldn't define a model for it. when my code is true, it will send a code for a mobile number and after that it doesn't navigate to another page. Here it is:

onTap: () {
                                      final ID = _controller.text;
                                      final url = "https://************/api/User/CheckHasUser?id=$ID";
                                      Future fetchLogin() async {
                                        final response = await http.get(url);
                                        if (response.statusCode == 200) {
                                          return jsonDecode(response.body);
                                        } else {
                                          throw Exception('Failed');
                                        }
                                      }
                                      var Id = ID != null ? true : false;
                                      FutureBuilder(
                                        future: fetchLogin(),
                                        builder: (context, snapshot){
                                          if(Id == true){
                                            Navigator.of(context).push(MaterialPageRoute(
                                              builder: (context) => getCodePage(),
                                            ));
                                          }else{
                                            print('s');
                                          }
                                        },
                                      );
                                    },

FutureBuilder is a Widget for reacting on Future results or showing a loading animation while waiting for it. Its builder-Function should return a Widget depending on the snapshot. But your FutureBuilder is not anywhere in the Widget tree so it is not built and does nothing. If you just want to react on the completion of a Future and its result, use fetchLogin().then(...) instead.

EDIT: Your code without FutureBuilder:

    final ID = _controller.text;
    final url = "https://************/api/User/CheckHasUser?id=$ID";
    Future fetchLogin() async {
        final response = await http.get(url);
        if (response.statusCode == 200) {
           return jsonDecode(response.body);
        } else {
           throw Exception('Failed');
        }
   }
   var Id = ID != null;
   fetchLogin().then((resultOfFuture){
     if(Id){
        Navigator.of(context).push(
           MaterialPageRoute(
              builder: (context) => getCodePage(),
           ));
        }else{
           print('s');
        }
     }
   );

I hope this helps you further:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM