Home > Net >  Flutter async function not synchronized
Flutter async function not synchronized

Time:05-23

I have this code:

APICalls ac = APICalls();

  String getCurrentUser() {
    return ac.getCurrentUser();
  }

  bool pass = false;
  void postPassword(String idProfile) async {
    final response = await ac.postItem("/v1/users/:0/pw", [
      idProfile
    ], {
      "old": oldPasswordController.text,
      "new": newPassword1Controller.text
    });
    if (response.statusCode >= 200 && response.statusCode < 300) {
      print("estoy en el if");
      pass = true;
      String accessToken = json.decode(response.body)['access_token'];
      String userID = json.decode(response.body)['id'];
      String refreshToken = json.decode(response.body)['refresh_token'];
      ac.initialize(userID, accessToken, refreshToken, true);
    } else if (response.statusCode == 400) {
      print("estoy en el else if");
      pass = false;
    } else {
      print("estoy en el else");
    }
  }

  bool correctChange() {
    postPassword(getCurrentUser());
    return pass;
  }

And I call it there:

: (!correctChange())
                                        ? showDialog(
                                            context: context,
                                            builder: (context) => AlertDialog(
                                                    title: const Text('Error'),
                                                    content: const Text(
                                                        'Incorrect old password'),
                                                    actions: [
                                                      TextButton(
                                                        child: const Text('OK'),
                                                        onPressed: () =>
                                                            Navigator.pop(
                                                                context),
                                                      )
                                                    ]))
                                        : showDialog(
                                            context: context,
                                            builder: (context) => AlertDialog(
                                                    title:
                                                        const Text('Correct'),
                                                    content: const Text(
                                                        'Password changed correctly'),
                                                    actions: [
                                                      TextButton(
                                                        child: const Text('OK'),
                                                        onPressed: () =>
                                                            Navigator.pop(
                                                                context),
                                                      )
                                                    ]));

The code above is a part where I look for other conditions. However, here is where I call the async function. I can't put everything inside a FutureBuilder because I need to make the call when I look for other things. How could I do it?

CodePudding user response:

Try this:


Future<bool> postPassword(String idProfile) async {
  bool pass = false; // this is moved INSIDE !
  
  // set pass to true or false accordingly in if-else-clauses

  return pass;
}

 Future<bool> correctChange() async {
    return await postPassword(getCurrentUser());
  }
blaBlaBla() async {

  // ... your code

  : await !correctChange() 

  // ... more of your code

}

CodePudding user response:

Call any asynchronous function with await keyword.

  • Related