Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: setStyleJSON not working after upgrading plugin version 2+ #601

Open
felixkrautschuk opened this issue Jun 28, 2024 · 2 comments
Open

Comments

@felixkrautschuk
Copy link

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

@felixkrautschuk
Copy link
Author

felixkrautschuk commented Jul 18, 2024

I just noticed that the same PlatformException also occurs on iOS:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(0, Source source_lines already exists, null, null)
#0 StyleManager.addStyleSource (package:mapbox_maps_flutter/src/pigeons/map_interfaces.dart:5816:7)

#1 StyleSource.addSource. (package:mapbox_maps_flutter/src/style/style.dart:317:66)

#2 MapPageState._onMapStyleLoaded (package:flutter_mapbox/pages/map-page.dart:54:5)

@felixkrautschuk
Copy link
Author

on iOS, the issue does not occur when setting styleUri to an empty string

MapWidget(
          //...
          styleUri: "",
          //...
),

but that does not help on Android

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant