From ec289f1eb41b525033bd5701a53a5c32d449059a Mon Sep 17 00:00:00 2001 From: Gabriel Terwesten Date: Wed, 8 Feb 2023 19:51:50 +0100 Subject: [PATCH] Don't call `PlatformViewCreatedCallback`s after `AndroidViewController` is disposed (#116854) * Don't call `PlatformViewCreatedCallback`s after `AndroidViewController` is disposed Before this change it was possible that, if a `AndroidViewController` was disposed before we got the notification that the platform view was created, `PlatformViewCreatedCallback`s where called even after calling `AndroidViewController.dispose`. Also makes `_PlatformViewLinkState._onPlatformViewCreated` more carful to only call `setState` when mounted. Closes #84628 Closes #96384 * Allow all widgets to remove listeners from controller * Remove assert * Add expectations to test --- .../lib/src/services/platform_views.dart | 11 ++++---- .../lib/src/widgets/platform_view.dart | 22 ++++++++++++---- .../test/services/platform_views_test.dart | 13 ++++++++++ .../test/widgets/platform_view_test.dart | 26 +++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/services/platform_views.dart b/packages/flutter/lib/src/services/platform_views.dart index fb903dd4a6..d2feeb8603 100644 --- a/packages/flutter/lib/src/services/platform_views.dart +++ b/packages/flutter/lib/src/services/platform_views.dart @@ -863,7 +863,7 @@ abstract class AndroidViewController extends PlatformViewController { Future setLayoutDirection(TextDirection layoutDirection) async { assert( _state != _AndroidViewState.disposed, - 'trying to set a layout direction for a disposed UIView. View id: $viewId', + 'trying to set a layout direction for a disposed Android view. View id: $viewId', ); if (layoutDirection == _layoutDirection) { @@ -938,12 +938,13 @@ abstract class AndroidViewController extends PlatformViewController { /// disposed. @override Future dispose() async { - if (_state == _AndroidViewState.creating || _state == _AndroidViewState.created) { + final _AndroidViewState state = _state; + _state = _AndroidViewState.disposed; + _platformViewCreatedCallbacks.clear(); + PlatformViewsService._instance._focusCallbacks.remove(viewId); + if (state == _AndroidViewState.creating || state == _AndroidViewState.created) { await _sendDisposeMessage(); } - _platformViewCreatedCallbacks.clear(); - _state = _AndroidViewState.disposed; - PlatformViewsService._instance._focusCallbacks.remove(viewId); } } diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index 5c5824d406..34026a290e 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -485,7 +485,7 @@ class _AndroidViewState extends State { _layoutDirection = newLayoutDirection; if (widget.viewType != oldWidget.viewType) { - _controller.dispose(); + _controller.disposePostFrame(); _createNewAndroidView(); return; } @@ -890,7 +890,7 @@ class _PlatformViewLinkState extends State { super.didUpdateWidget(oldWidget); if (widget.viewType != oldWidget.viewType) { - _controller?.dispose(); + _controller?.disposePostFrame(); // The _surface has to be recreated as its controller is disposed. // Setting _surface to null will trigger its creation in build(). _surface = null; @@ -911,9 +911,11 @@ class _PlatformViewLinkState extends State { } void _onPlatformViewCreated(int id) { - setState(() { - _platformViewCreated = true; - }); + if (mounted) { + setState(() { + _platformViewCreated = true; + }); + } } void _handleFrameworkFocusChanged(bool isFocused) { @@ -1209,3 +1211,13 @@ class _PlatformViewPlaceHolder extends SingleChildRenderObjectWidget { renderObject.onLayout = onLayout; } } + +extension on PlatformViewController { + /// Disposes the controller in a post-frame callback, to allow other widgets to + /// remove their listeners before the controller is disposed. + void disposePostFrame() { + SchedulerBinding.instance.addPostFrameCallback((_) { + dispose(); + }); + } +} diff --git a/packages/flutter/test/services/platform_views_test.dart b/packages/flutter/test/services/platform_views_test.dart index 2750bfcab8..9722c540fc 100644 --- a/packages/flutter/test/services/platform_views_test.dart +++ b/packages/flutter/test/services/platform_views_test.dart @@ -268,6 +268,19 @@ void main() { await controller2.create(size: const Size(100.0, 200.0)); expect(createdViews, orderedEquals([0, 5])); + final AndroidViewController controller3 = PlatformViewsService.initAndroidView( + id: 10, + viewType: 'webview', + layoutDirection: TextDirection.ltr, + )..addOnPlatformViewCreatedListener(callback); + expect(createdViews, orderedEquals([0, 5])); + + await Future.wait(>[ + controller3.create(size: const Size(100.0, 200.0)), + controller3.dispose(), + ]); + + expect(createdViews, orderedEquals([0, 5])); }); test("change Android view's directionality before creation", () async { diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index 0c9397cdd3..2ce07061cb 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -2718,6 +2718,32 @@ void main() { expect(disposedController.disposed, true); }); + testWidgets('PlatformViewLink handles onPlatformViewCreated when disposed', (WidgetTester tester) async { + late PlatformViewCreationParams creationParams; + late FakePlatformViewController controller; + final PlatformViewLink platformViewLink = PlatformViewLink( + viewType: 'webview', + onCreatePlatformView: (PlatformViewCreationParams params) { + creationParams = params; + return controller = FakePlatformViewController(params.id); + }, + surfaceFactory: (BuildContext context, PlatformViewController controller) { + return PlatformViewSurface( + gestureRecognizers: const >{}, + controller: controller, + hitTestBehavior: PlatformViewHitTestBehavior.opaque, + ); + }, + ); + + await tester.pumpWidget(platformViewLink); + + await tester.pumpWidget(Container()); + + expect(controller.disposed, true); + expect(() => creationParams.onPlatformViewCreated(creationParams.id), returnsNormally); + }); + testWidgets('PlatformViewLink widget survives widget tree change', (WidgetTester tester) async { final GlobalKey key = GlobalKey(); final int currentViewId = platformViewsRegistry.getNextPlatformViewId();