如何解决无法从Future返回对象列表
我正在执行一个Firebase查询,我想通过我的for循环返回一个对象影响的列表。但是该过程在for循环中停止了,并且没有返回任何东西。我还使用了get包来在屏幕上显示数据将Furture函数转换为流数据之后。
现在我想知道如何在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
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。