I have a problem about getting a marker on the map. I want to get a marker from location that I stored in Firebase.
This is my code that I get problem.
const _marker = 350.0;
class MapWidget extends StatefulWidget {
MapWidget({Key key, @required this.mapController}) : super(key: key);
final Completer<GoogleMapController> mapController;
@override
_MapWidgetState createState() => _MapWidgetState();
}
class _MapWidgetState extends State<MapWidget> {
List<Store> _store;
Completer<GoogleMapController> mapController;
@override
void initState() {
StoreNotifier storeNotifier =
Provider.of<StoreNotifier>(context, listen: false);
getStores(storeNotifier);
super.initState();
}
static final CameraPosition _initialPosition = CameraPosition(
target: LatLng(13.655258306757673, 100.49825516513702),
zoom: 15,
);
@override
Widget build(BuildContext context) {
StoreNotifier storeNotifier = Provider.of<StoreNotifier>(context);
return
// GoogleMap(
// initialCameraPosition: _initialPosition,
// markers: {
// Marker(
// markerId: MarkerId(storeNotifier.storeList[index].storeId),
// icon: BitmapDescriptor.defaultMarkerWithHue(_marker),
// position: LatLng(storeNotifier.storeList[index].location.latitude,
// storeNotifier.storeList[index].location.longitude),
// infoWindow: InfoWindow(
// title: storeNotifier.storeList[index].name,
// snippet: storeNotifier.storeList[index].address))
// },
// onMapCreated: (mapController) {
// this.mapController.complete(mapController);
// });
}
}
I comment the code that GoogleMap() because I cannot declare the index. I used to use provider to fetch data list, but I don't know how to fetch the marker on the map by using provider.
This is Notifier code.
class StoreNotifier with ChangeNotifier {
List<Store> _storeList = [];
UnmodifiableListView<Store> get storeList => UnmodifiableListView(_storeList);
set storeList(List<Store> storeList) {
_storeList = storeList;
notifyListeners();
}
}
This is code that I fetch data from Firebase.
getStores(StoreNotifier storeNotifier) async {
QuerySnapshot snapshot =
await FirebaseFirestore.instance.collection('stores').get();
List<Store> _storeList = [];
snapshot.docs.forEach((document) {
Store store = Store.fromMap(document.data());
_storeList.add(store);
});
storeNotifier.storeList = _storeList;
}
Model
class Store {
String storeId;
String name;
String address;
String image;
GeoPoint location;
Store();
Store.fromMap(Map<String, dynamic> data) {
storeId = data['storeId'];
name = data['name'];
address = data['address'];
image = data['image'];
location = data['location'];
}
}
I don't know how to fix it. I need your help :(