I created stateful widget that is used throughout the app to display a map with markers. I need to show a map on a screen in order to give users a better experience. I'm only writing this because SO says that I have too much code in this question.
The map displays fine on the iOS emulator. Haven't checked on a real iOS device yet.
I'm using google_maps_flutter: ^2.2.1
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class GoogleMapWidget extends StatefulWidget {
final double lat;
final double lng;
final String name;
final String id;
const GoogleMapWidget({
Key? key,
required this.lat,
required this.lng,
required this.name,
required this.id,
}) : super(key: key);
@override
State<GoogleMapWidget> createState() => _GoogleMapWidgetState();
}
class _GoogleMapWidgetState extends State<GoogleMapWidget> {
final Completer<GoogleMapController> mapController = Completer();
List<Marker> markers = <Marker>[];
@override
Widget build(BuildContext context) {
markers.add(
Marker(
markerId: MarkerId(widget.id),
position: LatLng(widget.lat, widget.lng),
infoWindow: InfoWindow(title: widget.name),
),
);
return SizedBox(
width: 350.0,
height: 400.0,
child: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(widget.lat, widget.lng),
zoom: 15.0,
),
mapType: MapType.normal,
markers: Set<Marker>.of(markers),
onMapCreated: (GoogleMapController controller) {
mapController.complete(controller);
},
),
);
}
}
The widget is used like this:
GoogleMapWidget(
name: name,
id: 'single_screen',
lat: lat,
lng: lng,
),
I connected a Samsung S10 to the computer and did flutter run
When entering the screen that uses the map widget I get this:
D/OpenGLRenderer( 4700): eglCreateWindowSurface
E/FrameEvents( 4700): updateAcquireFence: Did not find frame.
And when exiting:
V/GoogleMapController( 4700): Controller was disposed before GoogleMap was ready.
E/PlatformViewsController( 4700): Disposing platform view threw an exception
E/PlatformViewsController( 4700): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.isEmpty()' on a null object reference
E/PlatformViewsController( 4700): at com.google.android.gms.dynamic.DeferredLifecycleHelper.zae(com.google.android.gms:play-services-base@@18.0.1:1)
E/PlatformViewsController( 4700): at com.google.android.gms.dynamic.DeferredLifecycleHelper.onDestroy(com.google.android.gms:play-services-base@@18.0.1:2)
E/PlatformViewsController( 4700): at com.google.android.gms.maps.MapView.onDestroy(com.google.android.gms:play-services-maps@@18.0.0:1)
E/PlatformViewsController( 4700): at io.flutter.plugins.googlemaps.GoogleMapController.destroyMapViewIfNecessary(GoogleMapController.java:891)
E/PlatformViewsController( 4700): at io.flutter.plugins.googlemaps.GoogleMapController.dispose(GoogleMapController.java:578)
E/PlatformViewsController( 4700): at io.flutter.plugin.platform.PlatformViewsController$1.dispose(PlatformViewsController.java:376)
E/PlatformViewsController( 4700): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.dispose(PlatformViewsChannel.java:135)
E/PlatformViewsController( 4700): at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:63)
E/PlatformViewsController( 4700): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/PlatformViewsController( 4700): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/PlatformViewsController( 4700): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/PlatformViewsController( 4700): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/PlatformViewsController( 4700): at android.os.Handler.handleCallback(Handler.java:938)
E/PlatformViewsController( 4700): at android.os.Handler.dispatchMessage(Handler.java:99)
E/PlatformViewsController( 4700): at android.os.Looper.loopOnce(Looper.java:226)
E/PlatformViewsController( 4700): at android.os.Looper.loop(Looper.java:313)
E/PlatformViewsController( 4700): at android.app.ActivityThread.main(ActivityThread.java:8663)
E/PlatformViewsController( 4700): at java.lang.reflect.Method.invoke(Native Method)
E/PlatformViewsController( 4700): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
E/PlatformViewsController( 4700): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
D/OpenGLRenderer( 4700): setSurface called with nullptr
D/OpenGLRenderer( 4700): setSurface() destroyed EGLSurface
D/OpenGLRenderer( 4700): destroyEglSurface
[✓] Flutter (Channel stable, 3.3.4, on macOS 12.6 21G115 darwin-x64, locale en-EN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[!] Android Studio (not installed)
[✓] VS Code (version 1.72.0)
[✓] Connected device (4 available)
[✓] HTTP Host Availability