Reputation: 1247
I made a function async
but still can't use await
expression. What am I wrong? My code is like this.
Future<void> _aFunction() async {
DocumentReference docRef = Firestore.instance.collection('collection').document(docId);
docRef.get().then((DocumentSnapshot docSnapShot) {
if (docSnapShot.exists) {
String ip = await Connectivity().getWifiIP();
Upvotes: 0
Views: 175
Reputation: 4365
That's because here is an internal (anonymous) function declaration inside then
, which is not async. Actually, await
keyword can be thought as a syntactic sugar over then
, so it would be convenient to refactor the function like this:
Future<void> _aFunction() async {
final DocumentSnapshot snapshot = await Firestore.instance.collection('collection').document(docId).get();
if (snapshot.exists) {
String ip = await Connectivity().getWifiIP();
// Rest of the `ip` variable handling logic function
}
// Rest of the function
}
Now await
keyword corresponds to the _aFunction
itself instead of an anonymous function declared inside _aFunction
, so it works as expected.
Upvotes: 1