0

I am simply fetching some data from API what I need is to show loader till the API is loaded. The issue is loader is working fine but I don't know how can I close it.

My code

   Future<http.Response>  _trySubmit3() async {
      final isValid2 = _formKey3.currentState.validate();
      FocusScope.of(context).unfocus();

      if (isValid2) {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return Center(child: CircularProgressIndicator(
                valueColor: new AlwaysStoppedAnimation<Color>(Color(0xff00abb5)),

              ),);
            });


        print(smsOTP.text);
        print(userConfirmPassword.text);


        var url = '123/set_password.php?email=${userEmail.text}&password=${userConfirmPassword.text}';
        print(url);
        http.Response res = await http.get(url,
          headers: <String, String>{
            'token': 'my token'
          },

        );
        var data = json.decode(res.body.toString());
        print(data);
        print(data['status']);
        if(data['status'].toString() == "success"){

           // I need to close the loader here


        }
      }
  • 3
    Does this answer your question? [How to dismiss flutter dialog?](https://stackoverflow.com/questions/50683524/how-to-dismiss-flutter-dialog) – Tim Klingeleers Dec 18 '20 at 12:10

2 Answers2

0

If you're looking for dismissing the Dialog that contains the CircularProgressIndicator, immediately after all the operations have been completed, you could use the following code:

Navigator.pop(context);

Which, implemented in your example, would look like:

Future<http.Response>  _trySubmit3() async {
   final isValid2 = _formKey3.currentState.validate();
   FocusScope.of(context).unfocus();

   if (isValid2) {
     showDialog(
         context: context,
         builder: (BuildContext context) {
           return Center(child: CircularProgressIndicator(
             valueColor: new AlwaysStoppedAnimation<Color>(Color(0xff00abb5)),
           ),);
         });


     print(smsOTP.text);
     print(userConfirmPassword.text);

     var url = '123/set_password.php?email=${userEmail.text}&password=${userConfirmPassword.text}';
     print(url);
     http.Response res = await http.get(url,
       headers: <String, String>{
         'token': 'my token'
       },

     );
     var data = json.decode(res.body.toString());
     print(data);
     print(data['status']);
     if(data['status'].toString() == "success"){
         Navigator.pop(context); // closing the Dialog with the CircularProgressIndicator
     }
   }
 } 
Stefano Amorelli
  • 4,553
  • 3
  • 14
  • 30
0

Welcome to Stackoverflow :)

You can do this by this trick Navigator.pop(context). I think you need to hide when its success on if condition you can do like this

    if(data['status'].toString() == "success"){
       Navigator.pop(context)
    }
Umaiz Khan
  • 1,319
  • 3
  • 20
  • 66