flutter-mapbox-gl / maps

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

addGeoJsonSource throws execption when using `await` keyword #1150

Closed ujas-m-simformsolutions closed 2 years ago

ujas-m-simformsolutions commented 2 years ago

As addGeoJsonSource is future, it makes sense to add the await keyword before it but when I use await it throws platform exception.

Here is the code snippet,

 void onMapCreated(MapboxMapController controller) {
    mapboxController = controller;
    if (mapboxMapType.isMapWithBoundaries) {
      _createMapWithBoundaries();
    } else if (mapboxMapType.isMapWithMarker) {
      _placeMarkerOnMap();
    }
  }
 Future<void> _createMapWithBoundaries() async {
    try {
      await mapboxController?.addGeoJsonSource(
        GeoJsonKeys.sourceId,
        <String, dynamic>{
          'type': 'FeatureCollection',
          'features': [
            {
              'type': 'Feature',
              'geometry': {
                'type': 'Polygon',
                'coordinates': landDetails?.boundaryCoordinates,
              }
            }
          ],
          'properties': <String, dynamic>{}
        },
      );
} catch(e){
//error
}
}
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 3.3.0-0.1.pre, on macOS 11.4 20F71 darwin-x64, locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 13.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.70.0)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

Without await, it works fine

ujas-m-simformsolutions commented 2 years ago

We should not call anything inside onMapCreated. Always call inside onStyleLoadedCallback or after it completes. Fixed by moving functions inside onStyleLoadedCallback.