fleaflet / flutter_map

A versatile mapping package for Flutter. Simple and easy to learn, yet completely customizable and configurable, it's the best choice for mapping in your Flutter app.
https://pub.dev/packages/flutter_map
BSD 3-Clause "New" or "Revised" License
2.74k stars 861 forks source link

[BUG] `Polygon`s and `Polyline`s don't update when `points` modified #1894

Closed tologonkudaiberdiuulu closed 3 months ago

tologonkudaiberdiuulu commented 4 months ago

What is the bug?

After adding new points to polygon, it's not updating automatically. You should explicitly add UniqueKey to Polygon widget.

How can we reproduce it?

To reproduce you can view min sample in github. To solve you can uncomment "// key: UniqueKey()," part in map_page.dart file

Do you have a potential solution?

No

Platforms

All

Severity

Obtrusive: Prevents normal functioning but causes no errors in the console

JaffaKetchup commented 4 months ago

This issue is occuring because the equality checking for elements is not working correctly. A temporary workaround is shown in the invalid issue https://github.com/flutter/flutter/issues/106743: use List.from to create a new list instance.

tologonkudaiberdiuulu commented 4 months ago

Then maybe when overriding "=" operator of Polygon object, you should check manually? Not using listEquals function.

JaffaKetchup commented 4 months ago

It's not due to that. Its because the list instance is shared, so the list on the oldWidget and newwidget are always the same.

tologonkudaiberdiuulu commented 4 months ago

I already tried to make like this

image

It's also not working. Only way to work is creating "mapsPolygons" variable also from new instance of List. Like this:

image

But it's too complicated, no?

JaffaKetchup commented 4 months ago

I generally wouldn't recommend doing what you're doing. Instead, use List<List<LatLng>>.map((coords) {}).toList() on the coordinates list, in the build method. This may also resolve the issue as well.

final coordinates = <List<LatLng>>[...];

// inside `build`
PolygonLayer(
    polygons: coordinates.map((c) => Polygon(points: c)).toList(),
),