【发布时间】:2023-01-25 06:41:30
【问题描述】:
我正在尝试将 Future<List> 转换为 List 以便我可以在 for 循环中使用它在谷歌地图上创建标记。但是,当我在一个函数中使用 await 转换它并将其返回给另一个函数时,第二个函数仍然认为它是 Future<List>。
这是我的代码:
class mapPage extends StatelessWidget {
late GoogleMapController mapController;
final LatLng _center = const LatLng(51.49759646223456, -0.22884194229223435);
// function that converts
Future<List<LatLng>> getLocations(context) async {
List<LatLng> locations = await buildLocations(context);
return locations;
}
Set<Marker> _createMarker(context) {
// attempt to retrieve result of converting function (where error occurs)
List<LatLng> locations = getLocations(context);
return {
Marker(
markerId: const MarkerId("marker_1"),
position: _center,
infoWindow: const InfoWindow(
title: "",
snippet: "",
),
onTap: () => Navigator.push(
context,
MaterialPageRoute(builder: (context) => listPage2()),
)),
Marker(markerId: MarkerId(stores[0]), position: locations[0]),
const Marker(
markerId: MarkerId("marker_2"),
position: LatLng(51.488348, -0.237461),
icon: BitmapDescriptor.defaultMarker,
),
};
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
markers: _createMarker(context)),
);
}
@override
Future<List<LatLng>> buildLocations(BuildContext context) async {
List<LatLng> locations = [];
var collection = FirebaseFirestore.instance.collection('StoreDatabase');
var querySnapshot = await collection.get();
for (var queryDocumentSnapshot in querySnapshot.docs) {
Map<String, dynamic> document = queryDocumentSnapshot.data();
LatLng location = document['Location'];
locations.add(location);
}
return locations;
}
Future<List<String>> buildStores(BuildContext context) async {
List<String> stores = [];
var collection = FirebaseFirestore.instance.collection('StoreDatabase');
var querySnapshot = await collection.get();
for (var queryDocumentSnapshot in querySnapshot.docs) {
Map<String, dynamic> document = queryDocumentSnapshot.data();
String store = document['storeName'];
stores.add(store);
}
return stores;
}
}
任何帮助深表感谢!
【问题讨论】:
标签: flutter firebase google-maps google-cloud-firestore future