【发布时间】:2021-02-09 21:05:00
【问题描述】:
我有一个函数可以检索作为 Future 的数据列表。
final CollectionReference _departmentHoursCollection =
FirebaseFirestore.instance.collection('departmentsHours');
Future<List<DepartmentHour>> isUnfinishedGK(List<String> sites) async {
sites.asMap().forEach((key, value) async {
QuerySnapshot departmentsHoursSnapshot = await _departmentHoursCollection
.where('siteName', isEqualTo: value)
.where('justification', isEqualTo: 'working')
.get();
if (departmentsHoursSnapshot.docs.isNotEmpty) {
departmentsHoursSnapshot.docs.asMap().forEach((key, value) async {
var document = await value.reference.get();
print(document.data());
_temporal.add(DepartmentHour.fromMap(document.data()));
});
}
});
print("size: ${_temporal.length}");
return _temporal;
}
但是当我最后打印尺寸时,它一直显示:
I/flutter (20592):大小:0
但是正在获取数据并将其添加到变量中:_temporal:
I/flutter (20592): {globalTime: 24304, endHour: 06:00, startedTime: 时间戳(秒=1602329908,纳秒=453000000), departmentHourDetails: [{globalTime: 24300, endJobTime: 时间戳(秒=1602354212,纳秒=456000000),部门: Grind Room, startJobTime: Timestamp(seconds=1602329911, 纳秒=766000000)}, {globalTime: 24299, endJobTime: 时间戳(秒=1602354212,纳秒=456000000),部门:主要 生产,startJobTime:时间戳(秒=1602329913, 纳秒=94000000)}],开始时间:22:00,计划日期: 时间戳(秒=1602306000,纳秒=0),完成时间: 时间戳(秒=1602329908,纳秒=453000000),站点名称:SMD, 理由:工作}
任何人都知道如何使用获取的数据返回列表,而不是返回 []。
【问题讨论】:
-
这个
print(document.data());在输出中出现在print("size: ${_temporal.length}");之前吗 -
是的,该信息显示在 print("size:${temporal.length}"); 之前
-
尝试使用Future.forEach,我认为在循环完成之前返回列表。
-
这就是问题所在 :) 我不知道 Future.forEach 的存在,请发表您的评论作为答案,以便我接受 :)