Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The await expression can only be used in an async function

I am trying to load some data from Firestore when the user logs in and need to wait until the data is loaded before continuing. However, my efforts to await the results are not working.

Future<bool> userListener() async {
  _auth.onAuthStateChanged.listen((firebaseUser) {
    bool isDone = false; 

    if (firebaseUser != null) {
      isDone = await loadUserData(firebaseUser);   // This line is killing me!!
      return isDone;
    } else {
      return false;
    }
  }, onError: (error) {
    print('error in UserListener: $error');
  });
}

Future<bool> loadUserData(FirebaseUser user) async {
  Firestore database = Firestore();
  DocumentReference data = database.collection('userData').document(user.uid);

  try {
    DocumentSnapshot myQuery = await data.get();
    theNumber = myQuery.data['theNumber'];
    return true;
  } on PlatformException catch (e) {
    print(e);
    return false;
  }
}

I get a lovely error from the line:

 isDone = await loadUserData(firebaseUser);

that "the await expression can only be used in an async function".

I'm not sure how these functions could get any more async. Further, if I don't add await then I am told that a Future can't be assigned to a bool, which makes perfect sense, and suggest to me that my function is, in fact, async.

Why can't I await the results of my loadUserData function?

like image 951
August W Danowski Avatar asked Feb 10 '20 16:02

August W Danowski


People also ask

Can await only be used in async function?

The "await is only valid in async functions" error occurs when the await keyword is used inside of a function that wasn't marked as async . To use the await keyword inside of a function, mark the directly enclosing function as async .

Can await be used without async?

You can use the await keyword on its own (outside of an async function) within a JavaScript module. This means modules, with child modules that use await , wait for the child module to execute before they themselves run.

Why is await used with async?

Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.


2 Answers

your main function for that line is that passed to listen of onAuthStateChanged.this function should be async too like below

  _auth.onAuthStateChanged.listen((firebaseUser) async{
like image 67
Majid Sadrayi Avatar answered Oct 01 '22 14:10

Majid Sadrayi


If you are calling an async function, your calling function should be async too.

Add the async keyword to your function. Change:

void main (List<String> arguments)

to:

void main (List<String> arguments) async

Examples:

Future<void> main (List<String> arguments) async {
  var done = await readFileByteByByte(); // perform long operation
}

Future<void> main(List<String> arguments) {
  readFileByteByByte().then((done) {
    print('done');
  });
  print('waiting...');
  print('do something else while waiting...');
}
like image 28
live-love Avatar answered Oct 01 '22 16:10

live-love