mapbox / mapbox/mapbox-maps-flutter

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

Open
#887 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Dart
Stars
380
Forks
204
PR merge metrics
No merged PRs in 30d

Description

**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("");
runApp(MaterialApp(
home: InitialPage(),
));
}

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

class _InitialPageState extends State {
@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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Android crash with MapWidget using textureView: false and immediate Navigator.pushReplacement, then start from the reported Flutter platform-view stack trace: SurfaceProducerPlatformViewRenderTarget, VirtualDisplayController, and PlatformViewsController resize. Compare behavior with textureView: true and verify that removing the map during the transition no longer causes a NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.