Skip to content

Commit dde482d

Browse files
josxhaCopilot
andauthored
fix: web asset path resolution for Flutter assets in release mode (#343)
Co-authored-by: Copilot <[email protected]>
1 parent 4af74b8 commit dde482d

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

lib/src/platform/web/map_state.dart

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ final class MapLibreMapStateWeb extends MapLibreMapState {
5555
_map = interop.JsMap(
5656
interop.MapOptions(
5757
container: _htmlElement,
58-
style: _styleAsJsonOrUrl(options.initStyle),
58+
style: _prepareStyleString(options.initStyle),
5959
zoom: options.initZoom,
6060
center: options.initCenter?.toLngLat(),
6161
bearing: options.initBearing,
@@ -480,12 +480,28 @@ final class MapLibreMapStateWeb extends MapLibreMapState {
480480
.toList(growable: false);
481481
}
482482

483-
JSAny _styleAsJsonOrUrl(String styleString) {
484-
JSAny? ret;
485-
if (styleString.startsWith('{') || styleString.startsWith('[')) {
486-
ret = (jsonDecode(styleString) as Map<String, dynamic>).jsify();
483+
JSAny _prepareStyleString(String style) {
484+
final trimmed = style.trim();
485+
if (trimmed.startsWith('{')) {
486+
// Raw JSON
487+
final json = jsonDecode(trimmed) as Map<String, dynamic>;
488+
final jsified = json.jsify();
489+
if (jsified == null) {
490+
throw StateError('Failed to convert style JSON to JS object.');
491+
}
492+
return jsified;
493+
} else if (trimmed.startsWith('/')) {
494+
// path
495+
return trimmed.toJS;
496+
} else if (!trimmed.startsWith('http://') &&
497+
!trimmed.startsWith('https://') &&
498+
!trimmed.startsWith('mapbox://')) {
499+
// flutter asset
500+
return AssetManager().getAssetUrl(trimmed).toJS;
501+
} else {
502+
// URI
503+
return trimmed.toJS;
487504
}
488-
return ret ?? styleString.toJS;
489505
}
490506

491507
@override
@@ -496,7 +512,7 @@ final class MapLibreMapStateWeb extends MapLibreMapState {
496512
_onStyleLoaded();
497513
}.toJS,
498514
);
499-
_map.setStyle(_styleAsJsonOrUrl(style));
515+
_map.setStyle(_prepareStyleString(style));
500516
}
501517

502518
void _onStyleLoaded() {

0 commit comments

Comments
 (0)