mapbox-maps-flutter icon indicating copy to clipboard operation
mapbox-maps-flutter copied to clipboard

`style.addSource` throws a `PlatformException` but `style.getSource` does not return the existing source

Open Daeon97 opened this issue 1 year ago • 2 comments

I have

late final Source? source;

try {
  source = await style.getSource(
    'sourceId',
  );
} catch (_) {
    source = null;
}

Which returns a null on the ID sourceId. However when I try to add the source using the same ID sourceId

final geoJsonFeatureCollection = ...

final newSource = GeoJsonSource(
  id: 'sourceId',
  data: json.encode(
    geoJsonFeatureCollection,
  ),
);

await style.addSource(
  newSource,
);

I get a PlatformException(0, Source sourceId already exists, null, null)

I can't seem to get why this may be happening. Any pointers will be highly appreciated

PS: I am currently using version 2.3.0 of this library

Daeon97 avatar Jan 21 '25 08:01 Daeon97

I have modified my code to use a try {...} catch(...) {...} block at the point where the PlatformException is thrown. I now have

try {
  await style.addSource(
    newSource,
  );
  } catch (_) {
  await style.removeStyleSource(
    'sourceId',
  );
  
  await style.addSource(
    newSource,
  );
}

However the line

await style.removeStyleSource(
  'sourceId',
);

throws a PlatformException(0, Source sourceId is not in style, null, null)

But if sourceId is not in style why is

await style.addSource(
  newSource,
);

throwing a PlatformException(0, Source sourceId already exists, null, null) in the first place. These are 2 contradictory error messages. I have no idea what is happening here

Daeon97 avatar Jan 21 '25 12:01 Daeon97

I just upgraded to the latest version of this library as of now, Tuesday January 21st 2025 which is version 2.5.1. I am still experiencing this problem. I have also verified that this issue is happening on both Android and iOS

Daeon97 avatar Jan 21 '25 15:01 Daeon97