Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

Dart Future async not working as expected

I'm getting an

'await' can only be used in 'async' or 'async*' methods.

In popup error:

The await expression can only be used in an async function. Try marking the function body with either 'async' or 'async*'.dart(await_in_wrong_context)

firestore.dart

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:workout/main.dart';

class FirestoreViewModel {
  Future<List<int>> getDocuments(
      String type, int numValues, bool isDescending) async {
    List<int> _list;
    QuerySnapshot documents = await FirebaseFirestore.instance
        .collection('workouts')
        .where('id', isEqualTo: googleSignIn.currentUser.id)
        .orderBy('date', descending: isDescending)
        .limit(numValues)
        .get();

    _list = documents.docs.map((snapshot) => snapshot['water'] as int).toList();
    return _list;
  }
}

file.dart

import 'package:workout/services/firestore_view_model.dart';

List<int> _waters = await FirestoreViewModel().getDocuments("water", 7, true);

I just can't seem to get a list with all the Futures, awaits, and asyncs going around. I've looked at other solutions on here but they are set up a bit differently. Either way, I don't know how to create a method that returns a list, when the firestore is full of futures. Any ideas would be appreciated.

question from:https://stackoverflow.com/questions/65648913/dart-future-async-not-working-as-expected

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...