flutter - 无法从 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
}
解决方案
推荐阅读
- r - 以编程方式重命名 dplyr 中的列
- ios - 如何从另一个视图控制器重置表格视图单元格内容
- azure-devops - 在 VSTS Git 构建中,Get Sources 可以获取一个文件夹而不是整个仓库吗?
- uwp - 如何在 UWP Pivot 中检查页面是否为活动页面
- apache-spark - 当 Spark 序列化器与 Dataframe API 一起使用时
- html - 过渡css时文本模糊
- scala - 为什么我不能运行这样的 scala 代码?(reduceRight List 类方法)
- qt - QT Quick XmlListModel 仍然是空的
- git - 如何将git历史从一个分支连接到另一个分支?
- sed - 将文件作为带有临时更改文本的命令行参数传递