Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for disposal crash with flutter3 #1172

Merged
merged 10 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ xml ...

[Recommended](https://docs.mapbox.com/help/tutorials/first-steps-ios-sdk/#display-the-users-location) explanation about "Shows your location on the map and helps improve the map".

## Flutter 3.x.x issues and experimental workarounds
Since Flutter 3.x.x was introduced, it exposed some race conditions resulting in occasional crashes upon map disposal. The parameter `useDelayedDisposal` was introduced as a workaround for this issue until Flutter and/or Mapbox fix this issue properly. Use with caution - this is not yet production ready since several users still report crashes after using this workaround.

## Running the example code
See the [documentation about this topic](doc/RUNNING_EXAMPLE_CODE.md)

Expand Down
1 change: 0 additions & 1 deletion example/lib/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:async';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/material.dart';
Expand Down
1 change: 0 additions & 1 deletion example/lib/place_fill.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:async';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/material.dart';
Expand Down
1 change: 0 additions & 1 deletion example/lib/place_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:async';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/material.dart';
Expand Down
1 change: 0 additions & 1 deletion example/lib/place_symbol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'dart:async'; // ignore: unnecessary_import
import 'dart:core';
import 'dart:math';
// ignore: unnecessary_import
import 'dart:typed_data';

import 'package:flutter/material.dart';
Expand Down
13 changes: 12 additions & 1 deletion lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class MapboxMap extends StatefulWidget {
AnnotationType.line,
AnnotationType.circle,
],
this.useDelayedDisposal,
this.useHybridCompositionOverride,
}) : assert(annotationOrder.length <= 4),
assert(annotationConsumeTapEvents.length > 0),
super(key: key);
Expand Down Expand Up @@ -222,6 +224,13 @@ class MapboxMap extends StatefulWidget {
/// * All fade/transition animations have completed
final OnMapIdleCallback? onMapIdle;

/// Use delayed disposal of Android View Controller to avoid flutter 3.x.x crashes
/// Use with caution - this is not yet production ready since several users still report crashes after using this workaround
final bool? useDelayedDisposal;

/// Override hybrid mode per map instance
final bool? useHybridCompositionOverride;

/// Set `MapboxMap.useHybridComposition` to `false` in order use Virtual-Display
/// (better for Android 9 and below but may result in errors on Android 12)
/// or leave it `true` (default) to use Hybrid composition (Slower on Android 9 and below).
Expand Down Expand Up @@ -251,7 +260,9 @@ class _MapboxMapState extends State<MapboxMap> {
'options': _MapboxMapOptions.fromWidget(widget).toMap(),
'accessToken': widget.accessToken,
'onAttributionClickOverride': widget.onAttributionClick != null,
'dragEnabled': widget.dragEnabled
'dragEnabled': widget.dragEnabled,
'useDelayedDisposal': widget.useDelayedDisposal,
'useHybridCompositionOverride': widget.useHybridCompositionOverride,
};
return _mapboxGlPlatform.buildView(
creationParams, onPlatformViewCreated, widget.gestureRecognizers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

part 'src/view_wrappers.dart';
part 'src/annotation.dart';
part 'src/callbacks.dart';
part 'src/camera.dart';
Expand Down
45 changes: 35 additions & 10 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
OnPlatformViewCreatedCallback onPlatformViewCreated,
Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers) {
if (defaultTargetPlatform == TargetPlatform.android) {
if (useHybridComposition) {
final useDelayedDisposalParam =
(creationParams['useDelayedDisposal'] ?? false) as bool;
final useHybridCompositionParam =
(creationParams['useHybridCompositionOverride'] ??
useHybridComposition) as bool;
if (useHybridCompositionParam) {
return PlatformViewLink(
viewType: 'plugins.flutter.io/mapbox_gl',
surfaceFactory: (
Expand All @@ -156,15 +161,26 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
);
},
onCreatePlatformView: (PlatformViewCreationParams params) {
final SurfaceAndroidViewController controller =
PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
late AndroidViewController controller;
if (useDelayedDisposalParam) {
controller = WrappedPlatformViewsService.initAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
} else {
controller = PlatformViewsService.initAndroidView(
id: params.id,
viewType: 'plugins.flutter.io/mapbox_gl',
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
);
}
controller.addOnPlatformViewCreatedListener(
params.onPlatformViewCreated,
);
Expand All @@ -177,6 +193,15 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
},
);
} else {
if (useDelayedDisposalParam) {
return AndroidViewWithWrappedController(
viewType: 'plugins.flutter.io/mapbox_gl',
onPlatformViewCreated: onPlatformViewCreated,
gestureRecognizers: gestureRecognizers,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
);
}
return AndroidView(
viewType: 'plugins.flutter.io/mapbox_gl',
onPlatformViewCreated: onPlatformViewCreated,
Expand Down
Loading