bpillon / google_maps_cluster_manager

Simple Flutter clustering library for Google Maps
https://pub.dev/packages/google_maps_cluster_manager
MIT License
124 stars 111 forks source link

Force update needed after changing items list #2

Closed 0x7061 closed 3 years ago

0x7061 commented 4 years ago

Hi, thanks for your effort.

I'm haunted by a weird bug where after I update my items list, the initial new clusters are being updated but when zooming in, they aren't being unfolded anymore. They basically stay there until I again update the list.

So I digged into your code and found the forceUpdate setting, which seems to work. I'm not sure how performance intense this is but it's the only way to make it work.

Part of the widget

@override
Widget build(BuildContext context) {
  _ctx = context;

  return new StoreConnector<AppState, AppState>(
    onInit: (store) {
      // Set cluster items
      _items = store.state.companies.map((e) => ClusterItem(LatLng(e.lat, e.lon), item: e)).toList();
      // Set the manager
      _manager = ClusterManager<Company>(
        _items,
        _updateMarkers,
        markerBuilder: _markerBuilder,
        levels: [1, 3, 5, 7, 9, 12, 13, 15, 17, 20],
        initialZoom: _currentZoom,
      );   
    },
    onDidChange: (state) {
        // Pass new companies as items to manager
        _updateItems(state.companies); // <----------------
    },
  );
}

Update Items function

void _updateItems(List<Company> companies) {
  setState(() {
    _items = companies.map((e) => ClusterItem(LatLng(e.lat, e.lon), item: e)).toList();
    _manager.setItems(_items);
  });
}

Widget build part /w map

GoogleMap _map = GoogleMap(
  mapType: MapType.normal,
  myLocationButtonEnabled: false,
  initialCameraPosition: CameraPosition(...),
  markers: _markers,
  onMapCreated: (GoogleMapController controller) {
    _controller.complete(controller);
    _manager.setMapController(controller);
  },
  onCameraMove: (CameraPosition position) {
    _currentZoom = position.zoom;
    _manager.onCameraMove(position, forceUpdate: true);
  },
  onCameraIdle: _manager.updateMap,
);
bpillon commented 3 years ago

Hello @codepushr. I can't manage to reproduce it on the example (when you tap on the FAB, it update the items). Can you try to do it on your side ?