mapbox-maps-flutter icon indicating copy to clipboard operation
mapbox-maps-flutter copied to clipboard

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

Open Mojo1917 opened this issue 9 months ago • 2 comments

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

Mojo1917 avatar Mar 09 '25 21:03 Mojo1917

Hi @Mojo1917 thank you for the report! I believe the issue you are describing is the same as this bug reported in the Flutter SDK itself https://github.com/flutter/flutter/issues/154613. Could you add a comment to the thread there? I think a reproducible sample code would be much appreciated as well.

evil159 avatar Mar 10 '25 08:03 evil159

Hi @evil159, Thank you so much for sharing the issue. I did not see that before. Will add my sample code and questions there.

Mojo1917 avatar Mar 10 '25 08:03 Mojo1917