首页 > 解决方案 > 无法从 Future 返回对象列表

问题描述

我正在执行一个 firebase 查询,我想通过我的 for 循环返回一个受影响的对象列表。但是该过程在 for 循环中停止并且不返回任何内容。我还使用 get 包在投射后在屏幕上显示数据将函数转换为流数据。

现在我想知道如何在 Future 函数中很好地返回这个列表。如果你能告诉我如何,非常感谢。

Future<List<InfoCompagnie>> fetchCompanies(
    String depart, String arrivee, String jour, int place) async {
  var firstCaseDate = jour.inCaps;
  var codedDepart = depart.substring(0, 3).toUpperCase();
  var codedArrivee = arrivee.substring(0, 3).toUpperCase();
  List<InfoCompagnie> compagnieList = List<InfoCompagnie>();

  await db
      .collection('DISPONIBILITE')
      .document('FzxJ0eta1xIcSFyxd9nE')
      .get()
      .then((DocumentSnapshot event) {
    for (var compagnie in event.data[firstCaseDate]) {
      db
          .collection('PLACE')
          .document(compagnie)
          .collection('$codedDepart-$codedArrivee')
          .document(firstCaseDate)
          .collection('Bus')
          .where('PlaceDisponible', isGreaterThanOrEqualTo: 1)
          .getDocuments()
          .then((QuerySnapshot event) {
        if (event.documents.length > 0) {
          for (var data in event.documents) {
            var infoCompagnie = InfoCompagnie().obs;
            //Obtention du nomble de place disponible pour le voyage ici
            accessiblePlace = data.data['PlaceDisponible'];
            codeBus = data.data['CodeBus'];
            infoCompagnie.update((value) {
              value.accessiblePlace = accessiblePlace;
            });

            //Obtention de la classe du bus de voyage ici;
            db
                .collection('VEHICULE')
                .where('CodeBus', isEqualTo: codeBus)
                .getDocuments()
                .then((QuerySnapshot event) {
              for (var data in event.documents) {
                classe = data.data['Classe'];
                infoCompagnie.update((value) {
                  value.classe = classe;
                });
              }
            });

            //Obtention des info de la compagnie ici compagnie ici
            db
                .collection('COMPAGNIE')
                .where('Code', isEqualTo: compagnie)
                .getDocuments()
                .then((QuerySnapshot event) {
              for (var data in event.documents) {
                companieName = data.data['Nom'];
                companieCode = compagnie;
                infoCompagnie.update((value) async {
                  value.companieName = companieName;
                  value.companieCode = companieCode;
                  value.travelNumber = getTravelNumber(companieCode);
                });
              }
            });

            //Obtention de l'horaire de voyage ici
            db
                .collection('PLACE')
                .document(compagnie)
                .collection('$codedDepart-$codedArrivee')
                .getDocuments()
                .then((element) {
              for (var data in element.documents) {
                //Obtention du montant du ticket de voyage ici
                if (data.documentID == 'Montant') {
                  amount = data.data['Montant'];
                  infoCompagnie.update((value) {
                    value.amount = amount;
                  });
                } else if (data.documentID == 'Horaire') {
                  infoCompagnie.update((value) {
                    //Obtention de l'horaire de voyage ici
                    value.departureHoure = data.data['HeureDépart'];
                    value.arrivalHoure = data.data['HeureArrivée'];
                  });
                } else {
                  var days = {
                    'Lundi',
                    'Mardi',
                    'Mercredi',
                    'Jeudi',
                    'Vendredi',
                    'Samedi',
                    'Dimanche'
                  };
                  if (days.contains(data.documentID)) {
                    db
                        .collection('PLACE')
                        .document(compagnie)
                        .collection('$codedDepart-$codedArrivee')
                        .document(data.documentID)
                        .collection('Bus')
                        .where('PlaceDisponible', isGreaterThanOrEqualTo: 1)
                        .getDocuments()
                        .then((bus) {
                      if (bus.documents.length > 0) {
                        for (var busInfo in bus.documents) {
                          infoCompagnie.update((value) {
                            value.othersPlaces[data.documentID] =
                                busInfo.data['PlaceDisponible'];
                          });
                        }
                      }
                    });
                  }
                }
              }
            });
            compagnieList.add(infoCompagnie.value); //Adding to list here
          } //Fin for de compagnie
        } //Sortie de condition if
      });
    } //Fin for
  });
  return compagnieList; //Return my list here
}

标签: flutterdartgoogle-cloud-firestoreasync-await

解决方案


推荐阅读