【发布时间】:2021-06-20 18:58:36
【问题描述】:
我正在尝试调用一个函数,该函数将通过读取流来填充列表。意思是,它将读取一个流并将值添加到一个列表中,最后,该列表将由函数返回。
问题是,当我使用 await for 读取流时,await for 之后的代码没有被执行。而且我尝试使用听,然后它不会等待完成阅读流。甚至在流被完全读取之前,监听之后的代码就已经被执行了。
按照我的代码-
Future<List<Map<String, dynamic>>> getNearestDriver(Stream<List<DocumentSnapshot>> driverWithinRangeStream, GeoPoint source) async {
print("Get Nearest Driver Called");
List<Map<String, dynamic>> driverAvailableList = [];
await for (List<DocumentSnapshot> documentList in driverWithinRangeStream){
documentList.forEach((DocumentSnapshot document) async {
String driverIDVar = document.data['driverid'];
GeoPoint pos = document.data['position']['geopoint'];
var point = geo.point(latitude: pos.latitude, longitude: pos.longitude);
double distanceVar = point.distance(lat: source.latitude, lng: source.longitude);
int distanceInMeter = (distanceVar*1000).round();
Map<String, dynamic> newDriverAvailable = {"driverId": driverIDVar, "distance" : distanceInMeter};
driverAvailableList.add(newDriverAvailable);
});
print(driverAvailableList); /* <- I get the data here */
}
print(driverAvailableList); /* <- But I don't get the data here. This code is not even being executed. */
}
请告诉我这里出了什么问题。
【问题讨论】:
标签: flutter