Body:
I’m working on a Flutter app that uses the maplibre_gl
and flutter radar
packages to display a map. (https://radar.com/documentation/tutorials/displaying-radar-maps-with-flutter) I want to add a custom marker to the map using an image stored in the images
folder in the root directory. I’ve added the image path to the pubspec.yaml
file, and the marker image loads without any errors in development mode. However, the marker doesn’t appear on the map after the flutter run -d chrome
. Below are the details and the relevant logs.
Environment Details:
- Flutter version:
3.5.4
- Dependencies:
dependencies: flutter: sdk: flutter flutter_radar: ^3.12.3 flutter_map: ^5.0.0 latlong2: ^0.9.0 cupertino_icons: ^1.0.8 http: ^1.2.2 http_client: ^1.5.3 vector_map_tiles: ^6.0.2 maplibre_gl: ^0.20.0
- Asset Configuration in
pubspec.yaml
:flutter: assets: - images/marker.png
- Image Path:
I’ve placed the marker image in:project/images/marker.png
Code Implementation:
Here’s my onMapCreated
function and helper methods to load the marker:
Future<void> _onMapCreated(MapLibreMapController controller) async {
print('[DEBUG] Entering _onMapCreated');
try {
// Path for marker image
const imagePath = 'images/marker.png';
print('[DEBUG] Loading marker image from path: $imagePath');
// Load marker image
final Uint8List markerImage = await _loadMarkerImage(imagePath);
print('[DEBUG] Marker image loaded successfully');
// Add marker image to map
await controller.addImage('marker', markerImage);
print('[DEBUG] Marker image added to map');
// Add a marker to the map
const markerPosition = LatLng(40.7342891, -73.9910334); // Marker position
print('[DEBUG] Adding marker at: ${markerPosition.latitude}, ${markerPosition.longitude}');
await controller.addSymbol(
SymbolOptions(
geometry: markerPosition,
iconImage: 'marker', // Matches the key used in addImage()
iconSize: 1.0,
),
);
print('[DEBUG] Marker added successfully');
} catch (e, stackTrace) {
print('[ERROR] Exception in _onMapCreated: $e');
print(stackTrace);
}
}
Future<Uint8List> _loadMarkerImage(String path) async {
print('[DEBUG] Loading image from path: $path');
try {
final ByteData data = await rootBundle.load(path);
print('[DEBUG] Image loaded from assets: $path');
return data.buffer.asUint8List();
} catch (e) {
print('[ERROR] Failed to load marker image from path: $path');
throw Exception('[ERROR] Asset not found or invalid: $path');
}
}
What I See in the Logs:
Despite the image being loaded successfully, the marker isn’t added to the map, and I get the following logs:
1736194029179 [stdout] [DEBUG] Map created successfully
1736194029179 [stdout] [DEBUG] Entering _onMapCreated
1736194029179 [stdout] [DEBUG] Loading marker image
1736194029179 [stdout] [DEBUG] Entering _loadMarkerImage
1736194029180 [stdout] [DEBUG] Loading image from path: packages/portal_package/assets/images/marker.png
1736194029255 [stdout] [DEBUG] Image loaded from assets: packages/portal_package/assets/images/marker.png
1736194029255 [stdout] [DEBUG] Marker image loaded successfully
1736194029255 [stdout] [DEBUG] Adding marker image to map
1736194029262 [stdout] [DEBUG] Marker image added to map
1736194029262 [stdout] [DEBUG] Adding marker symbol to map at: 40.7342891, -73.9910334
1736194029263 [stdout] [ERROR] Exception in _onMapCreated: Unexpected null value.
What I’ve Tried:
- Ensured the
images/marker.png
file exists in the root directory and matches the path inpubspec.yaml
. - Confirmed the asset was declared correctly in
pubspec.yaml
and rebuilt the project (flutter clean
andflutter pub get
). - Verified the image loads successfully during the
_loadMarkerImage
step. - Tested with both
flutter run -d chrome
and the production build (flutter build web
).
Expected Behavior:
The marker image should display on the map.
Question:
Why is the marker not appearing on the map even though the image loads successfully? How can I fix the Unexpected null value
exception in the addSymbol
step?
2
Answers
Ok, I looked into the
maplibre_gl
package. All the examples in the docs are adding the symbols in theonStyleLoadedCallback
callback and not in theonMapCreated
.Take a look at this MapLibreMap class documetation. Even here it is suggested You should only add annotations (e.g. symbols or circles) after
onStyleLoadedCallback
has been called.So you should add the custom marker symbol in the
onStyleLoadedCallback
callback.The Radar documentation you pointed may be wrong in adding the Symbol in
onMapCreated
callback.Further reference: Check this Github comment
It appears the error happens during the
addSymbol
step despite image loading successfully. Use the implementation below which includes error handling and logging:Create an instance of
MapMarkerManager
in your map screen:Call the
addMarkerToMap
method:Here are some additional troubleshooting tips:
Make sure your marker image isn’t too large. A size of 32×32 or 64×64 pixels is recommended.
Check your pubspec.yaml formatting:
iconSize
parameter – sometimes the default size might be too small or large depending on your image.