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.68k stars 848 forks source link

[BUG] 7.0.1 MapOptions assertion fails even when new object instance is used #1914

Closed MobileDev500 closed 3 weeks ago

MobileDev500 commented 3 weeks ago

What is the bug?

I have FlutterMap inside Consumer widget (using Provider package) as this is required to set screen into loading state when waiting for API request response. When the screen is rebuilt using notifyListeners(), new instances of MapController and MapOptions are assigned to FlutterMap. However, upon first rebuild, this error is thrown:

The following assertion was thrown building Container:
Should not update options unless they change
'package:flutter_map/src/map/controller/map_controller_impl.dart':
Failed assertion: line 342 pos 7: 'newOptions != value.options'

I'm not sure how this is possible as the new instance of every object should not be equal to any other object. Here is my code (vm = ViewModel/ChangeNotifier):

FlutterMap(
                        mapController: mapController,
                        options: MapOptions(
                          initialCenter: vm.initialCenter,
                          initialZoom: 15.0,
                          onTap: (tapPosition, point) =>
                              vm.selectArea(point),
                          onMapReady: () {
                            vm.mapController = mapController;
                          },
                        ),
                        children: [
                          TileLayer(
                            urlTemplate:
                                'https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',
                            userAgentPackageName: 'com.example.myapp',
                          ),
                          if (vm.polygons.isNotEmpty)
                            PolygonLayer(
                              polygons: vm.polygons,
                            ),
                          if (vm.markers.isNotEmpty)
                            MarkerLayer(markers: vm.markers)
                        ],
                      )

How can we reproduce it?

Use Consumer widget in combination with ChangeNotifier to rebuild the following map:

FlutterMap(
                        mapController: MapController(),
                        options: MapOptions(
                          initialCenter: LatLng(0.0,0.0),
                          initialZoom: 15.0,
                        ),
                        children: [
                          TileLayer(
                            urlTemplate:
                                'https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',
                            userAgentPackageName: 'com.example.myapp',
                          ),
                        ],
                      )

Do you have a potential solution?

Remove assertion, or perhaps add key property to MapOptions so new object with same parameters can be attached?

Platforms

Android 14

Severity

Erroneous: Prevents normal functioning and causes errors in the console

josxha commented 3 weeks ago

Thanks for opening the issue. Have a look at:

Closing this issue as a duplicate.