在异步函数中返回值-Flutter

时间:2018-09-01 13:04:34

标签: firebase dart flutter google-cloud-firestore

使用Cloud Firestore作为我的数据库。 我需要从代码中的两个单独的文档中读取数据,并根据上一个文档中的值从一个文档中获取数据。

通过类似于以下方式的firestore中的数据来获取数据:

  Future<List<Map<String, dynamic>>> getGenAlertsData() async{

  QuerySnapshot alertRef = await Firestore.instance.collection('alerts').orderBy('time', descending: true).getDocuments();

  List<Map<String, dynamic>> messages = new List();
  Map<String, dynamic> fullMessage;

// int counter = 0;

  alertRef.documents.forEach((doc) async {
    // counter++;
    String senderKey, senderNameKey, senderSpecKey, senderPicKey, msgBodyKey, timeKey, sender, senderName, senderSpec, msgBody, senderPic;
    senderKey = 'sender';
    sender = doc['sender'];
    timeKey = 'time';
    DateTime time = doc['time'];
    print(time);
    msgBodyKey = 'msgBody';
    msgBody = doc['msgBody'];
    DocumentReference ref = Firestore.instance.document('/staff/'+sender);
    print(ref.documentID);

    await ref.get().then((onValue) { //values not populated here :(
      senderNameKey = 'senderName';
      senderName =  onValue['name'];
      print(senderName);
      senderPicKey = 'senderPic';
      senderPic =   onValue['profilePic'];
      senderSpecKey = 'specialization';
      senderSpec =  onValue['specialization'];
    });

    // await print(senderName);
    fullMessage = <String, dynamic> {
      senderKey: sender,
      senderNameKey: senderName,
      senderSpecKey: senderSpec,
      senderPicKey: senderPic,
      timeKey: time,
      msgBodyKey: msgBody,
    };


    messages.add(fullMessage);

    print(messages.toString()); // I get the messages printed in my console here

  }); //loop over collection ends here

  print(messages.toString()); // No data here :(

    return messages;

}

此时,只有alertRef.documents.forEach((doc)中的值才可用,并且await ref.get().then((onValue) {内部的值未填充(在fullMessage映射中为空)。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

是的!终于知道了!

很明显,那么then()在异步函数中不能很好地工作,现在我的代码看起来像这样,现在一切都很好! :)

将其全部放入一个很好的for循环中,现在一切正常。

Future<List<Map<String, dynamic>>> getGenAlertsData() async{

  QuerySnapshot alertRef = await Firestore.instance.collection('alerts').orderBy('time', descending: true).getDocuments();
  List<Map<String, dynamic>> messages = new List();
  Map<String, dynamic> fullMessage;
  // String xxx;

  String senderKey, senderNameKey, senderSpecKey, senderPicKey, msgBodyKey, timeKey, sender, senderName, senderSpec, msgBody, senderPic;


  List<DocumentSnapshot> alertSnaps= alertRef.documents;

  for (int i = 0; i < alertSnaps.length; i++)

  {
    senderKey = 'sender';
    sender = alertSnaps[i]['sender'];
    timeKey = 'time';
    DateTime time = alertSnaps[i]['time'];
    print(time);
    msgBodyKey = 'msgBody';
    msgBody = alertSnaps[i]['msgBody'];
    DocumentSnapshot snappy = await Firestore.instance.document('/staff/'+sender).get();
      senderNameKey = 'senderName';
      senderName = snappy['name'];
      print('Doc for sender' + senderName);
      senderPicKey = 'senderPic';
      senderPic =  snappy['profilePic'];
      senderSpecKey = 'specialization';
      senderSpec = snappy['specialization'];

      fullMessage = <String, dynamic> {
      senderKey: sender,
      senderNameKey: senderName,
      senderSpecKey: senderSpec,
      senderPicKey: senderPic,
      timeKey: time,
      msgBodyKey: msgBody,};

      messages.add(fullMessage);
  }

  return messages;


}