'Flutter - how to trigger a function after the async function is completed

Hi I would like for the addUserToFirestore() function to trigger after the code above has done being executed

Future<void> registerAccount(
      String email,
      String displayName,
      String password,
      void Function(FirebaseAuthException e) errorCallback) async {
    try {
      var credential = await FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
      await credential.user!.updateDisplayName(displayName);

      addUserToFirestore();
    } on FirebaseAuthException catch (e) {
      errorCallback(e);
    }
  }


Solution 1:[1]

try and change your code to:

 await credential.user!.updateDisplayName(displayName)
   .then((_) => addUserToFirestore());

so that addUserToFirestore() will only fire after updateDisplayName() is complete

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AJ989