MarkerUpdates.from constructor

MarkerUpdates.from(
  1. Set<Marker> previous,
  2. Set<Marker> current
)

根据之前的marker列表previous和当前的marker列表current创建MakerUpdates.

Implementation

MarkerUpdates.from(Set<Marker> previous, Set<Marker> current) {
  // ignore: unnecessary_null_comparison
  if (previous == null) {
    previous = Set<Marker>.identity();
  }

  // ignore: unnecessary_null_comparison
  if (current == null) {
    current = Set<Marker>.identity();
  }

  final Map<String, Marker> previousMarkers = keyByMarkerId(previous);
  final Map<String, Marker> currentMarkers = keyByMarkerId(current);

  final Set<String> prevMarkerIds = previousMarkers.keys.toSet();
  final Set<String> currentMarkerIds = currentMarkers.keys.toSet();

  Marker idToCurrentMarker(String id) {
    return currentMarkers[id]!;
  }

  final Set<String> _markerIdsToRemove =
      prevMarkerIds.difference(currentMarkerIds);

  final Set<Marker> _markersToAdd = currentMarkerIds
      .difference(prevMarkerIds)
      .map(idToCurrentMarker)
      .toSet();

  bool hasChanged(Marker current) {
    final Marker? previous = previousMarkers[current.id];
    return current != previous;
  }

  final Set<Marker> _markersToChange = currentMarkerIds
      .intersection(prevMarkerIds)
      .map(idToCurrentMarker)
      .where(hasChanged)
      .toSet();

  markersToAdd = _markersToAdd;
  markerIdsToRemove = _markerIdsToRemove;
  markersToChange = _markersToChange;
}