0

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 :(

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
eight
  • 79
  • 1
  • 6
  • You are trying to access a private list in your notifier. See the `underscore` in `_storeList `. Remove the `underscore and you can access `storeList`. Also, change the function name `storeList` as well. – Xihuny May 07 '21 at 10:17
  • Do you mean removing the underscore "_storeList" in everywhere that I wrote or just some place? @Xihuny – eight May 07 '21 at 11:50
  • You need to change from everywhere you have used it. Refactoring will do it automatically. – Xihuny May 07 '21 at 11:53
  • I have already changed it, but it still error on "storeNotifier.setStoreList[index].storeId" that the index is undefined. Sorry, I'm new to Flutter :( – eight May 07 '21 at 12:00
  • You need to define the index somewhere. You have a Store list which consists of many stores and whichever store you want to show, you need to give that store index in list. – Xihuny May 07 '21 at 16:07
  • Are you trying to show Single store or All the stores marker on the map? – Xihuny May 07 '21 at 16:07
  • All stores marker – eight May 07 '21 at 16:08
  • Then you need to give a list of Markers. Right now you are settings a single marker – Xihuny May 07 '21 at 16:13
  • Check this answer: https://stackoverflow.com/a/66014645/3454744 – Xihuny May 07 '21 at 16:14
  • Thank you so much. I try to follow the answer that you give me, but the markers don't appear. I'm not sure that my code is correct or not. – eight May 07 '21 at 16:55
  • This is my code that has a problem https://stackoverflow.com/q/67439082/14932235 – eight May 07 '21 at 17:08

0 Answers0