Skip to content

Crash on Android when MapWidget is quickly removed from the widget tree (textureView: false) #887

Open
@Mojo1917

Description

@Mojo1917

Mapbox Version: mapbox_maps_flutter: 2.6.1

Description

On Android devices, the Flutter app crashes when a MapWidget is added to the widget tree and then immediately removed. This occurs, for example, when a page displaying a map is shown on the initial app start but is quickly replaced by another route.

The crash seems to be related to textureView: false, as it does not occur when textureView: true is used.

I’ve tried debugging the issue but cannot determine the root cause. Does anyone know where this might be coming from?

Steps to Reproduce

  1. Start the app with a route that contains MapWidget.
  2. Immediately navigate to another route (e.g., using Navigator.pushReplacement).
  3. The app crashes on Android with a NullPointerException.

Example Code

import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';

void main() {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  MapboxOptions.setAccessToken("<TOKEN>");
  runApp(MaterialApp(
    home: InitialPage(),
  ));
}

class InitialPage extends StatefulWidget {
  @override
  _InitialPageState createState() => _InitialPageState();
}

class _InitialPageState extends State<InitialPage> {
  @override
  void initState() {
    super.initState();
    Future.delayed(Duration(milliseconds: 100), () {
      Navigator.pushReplacement(
          context, MaterialPageRoute(builder: (_) => AnotherPage()));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MapWidget(
        textureView: false,
        androidHostingMode: AndroidPlatformViewHostingMode.TLHC_VD,
      ),
    );
  }
}

class AnotherPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text("Another Page")),
    );
  }
}

Error logs

FATAL EXCEPTION: main
Process: XXX, PID: 15063
java.lang.NullPointerException: Attempt to invoke interface method 'int io.flutter.view.TextureRegistry$SurfaceProducer.getWidth()' on a null object reference
at io.flutter.plugin.platform.SurfaceProducerPlatformViewRenderTarget.getWidth(SurfaceProducerPlatformViewRenderTarget.java:25)
at io.flutter.plugin.platform.VirtualDisplayController.getRenderTargetWidth(VirtualDisplayController.java:132)
at io.flutter.plugin.platform.PlatformViewsController$1.lambda$resize$0$io-flutter-plugin-platform-PlatformViewsController$1(PlatformViewsController.java:351)
at io.flutter.plugin.platform.PlatformViewsController$1$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

Flutter doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.29.1, on macOS 15.3.1 24D70 darwin-arm64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.98.0)
[✓] Connected device (5 available)
[✓] Network resources

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions