flutter-mapbox-gl / maps

A Mapbox GL flutter package for creating custom maps
Other
1.04k stars 503 forks source link

How do I fix getting `_CastError (Null check operator used on a null value)` when using `controller.addCircle`? #1313

Closed eRuaro closed 1 year ago

eRuaro commented 1 year ago

I can't get a map to render using the example code as a basis. How do I fix this? It's saying that circleManager is null.

MapboxMap(
  initialCameraPosition: initialCameraPosition,
  accessToken: dotenv.env['MAPBOX_ACCESS_TOKEN']!,
  onMapCreated: (controller) async {
  mapController = controller;
   await controller.addCircle(
   CircleOptions(
      geometry: LatLng(
      state.locationData.latitude!,
      state.locationData.longitude!,
    ),
     circleRadius: 5,
     circleColor: "blue",
   ),
  );
},
);
dan-gandolfo commented 1 year ago

Hi,

To solve this issue, you have to move your code :

await controller.addCircle(
   CircleOptions(
      geometry: LatLng(
      state.locationData.latitude!,
      state.locationData.longitude!,
    ),
     circleRadius: 5,
     circleColor: "blue",
   ),
 );

in the onStyleLoadedCallback callback like this:

onStyleLoadedCallback: () async {
  await controller.addCircle(
   CircleOptions(
      geometry: LatLng(
      state.locationData.latitude!,
      state.locationData.longitude!,
    ),
     circleRadius: 5,
     circleColor: "blue",
   ),
  );
},

This solution run on my pixel 6a with Android 13.

dan-gandolfo commented 1 year ago

Also you need to add AnnotationType.circle to the annotationOrder MapboxMap property

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.