mapbox / mapbox/mapbox-maps-flutter
Android: setStyleJSON not working after upgrading plugin version 2+
Nobody has claimed this yet.
- Dominant language
- Dart
- Stars
- 380
- Forks
- 204
- PR merge metrics
- No merged PRs in 30d
Description
After upgrading the plugin from version 1.1.0 to 2.0.0, I have some issues when developing for Android.
I try to set a custom style json like this in the _onMapCreated_ callback:
```
_onMapCreated(MapboxMap mapboxMap) async {
this.mapboxMap = mapboxMap;
final styleJson = await rootBundle.loadString("assets/map-styles/my_style.json");
mapboxMap.style.setStyleJSON(styleJson);
print("done");
}
```
I see the log message in the console, so the code is executed as expected without any error, but I still see the default Mapbox style on Android. It is working as expected on iOS and it was also working on Android using plugin version 1.1.0.
Futhermore, when adding a layer in the _onStyleLoadedListener_ like this:
```
_onMapStyleLoaded(StyleLoadedEventData styleLoadedEventData) async {
final linesData = await rootBundle.loadString('assets/map-data/lines.json');
await mapboxMap?.style.addSource(GeoJsonSource(id: "source_lines", data: linesData));
await mapboxMap?.style.addLayer(
LineLayer(
id: "layer_lines",
sourceId: "source_lines"
)
);
}
```
I see the added layer, but I also see an exception on Android:
>
> E/flutter (22160): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(Throwable, java.lang.Throwable: Source source_lines already exists, Cause: null, Stacktrace: java.lang.Throwable: Source source_lines already exists
> E/flutter (22160): at com.mapbox.maps.mapbox_maps.StyleController.addStyleSource(StyleController.kt:288)
> E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion.setUp$lambda-51$lambda-50(MapInterfaces.kt:5972)
> E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion.$r8$lambda$DXAQ35E-7m0eWhGSNgtBkwfDJqQ(Unknown Source:0)
> E/flutter (22160): at com.mapbox.maps.mapbox_maps.pigeons.StyleManager$Companion$$ExternalSyntheticLambda8.onMessage(Unknown Source:2)
> E/flutter (22160): at io.flutter.plugin.common.BasicMessageChannel$IncomingMessageHandler.onMessage(BasicMessageChannel.java:261)
> E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:292)
> E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
> E/flutter (22160): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
> E/flutter (22160): at android.os.Handler.handleCallback(Handler.java:958)
> E/flutter (22160): at android.os.Handler.dispatchMessage(Handler.java:99)
> E/flutter (22160): at android.os.Looper.loopOnce(Looper.java:205)
> E/flutter (22160): at android.os.Looper.loop(Looper.java:294)
> E/flutter (22160): at android.app.ActivityThread.main(ActivityThread.java:8177)
> E/flutter (22160): at java.lang.reflect.Method.invoke(Native Method)
> E/flutter (22160): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
> E/flutter (22160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
> E/flutter (22160): , null)
> E/flutter (22160): #0 StyleManager.addStyleSource (package:mapbox_maps_flutter/src/pigeons/map_interfaces.dart:5816:7)
> E/flutter (22160):
> E/flutter (22160): #1 StyleSource.addSource. (package:mapbox_maps_flutter/src/style/style.dart:317:66)
> E/flutter (22160):
> E/flutter (22160): #2 MapPageState._onMapStyleLoaded (package:flutter_mapbox/main.dart:48:5)
> E/flutter (22160):
It is working as expected on iOS and it was also working on Android with plugin 1.1.0.
Full sample project:
[flutter_mapbox.zip](https://github.com/user-attachments/files/16030176/flutter_mapbox.zip)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked flutter_mapbox sample on Android and comparing behavior between plugin versions 1.1.0 and 2.0.0. Inspect the Android StyleController.addStyleSource path identified in the stack trace and the setStyleJSON/style-loaded callbacks. Done means the custom style is displayed and the source can be added without the duplicate-source exception.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, dart, flutter
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100