diff --git a/packages/flutter/lib/src/widgets/platform_view.dart b/packages/flutter/lib/src/widgets/platform_view.dart index 8356982198..27b0b977dd 100644 --- a/packages/flutter/lib/src/widgets/platform_view.dart +++ b/packages/flutter/lib/src/widgets/platform_view.dart @@ -846,6 +846,9 @@ class _PlatformViewLinkState extends State { if (widget.viewType != oldWidget.viewType) { _controller?.dispose(); + // The _surface has to be recreated as its controller is disposed. + // Setting _surface to null will trigger its creation in build(). + _surface = null; // We are about to create a new platform view. _platformViewCreated = false; diff --git a/packages/flutter/test/widgets/platform_view_test.dart b/packages/flutter/test/widgets/platform_view_test.dart index cc3fd902b8..a77b5c3ff3 100644 --- a/packages/flutter/test/widgets/platform_view_test.dart +++ b/packages/flutter/test/widgets/platform_view_test.dart @@ -2059,6 +2059,7 @@ void main() { testWidgets('PlatformViewLink re-initializes when view type changes', (WidgetTester tester) async { final int currentViewId = platformViewsRegistry.getNextPlatformViewId(); final List ids = []; + final List surfaceViewIds = []; final List viewTypes = []; PlatformViewLink createPlatformViewLink(String viewType) { @@ -2072,6 +2073,7 @@ void main() { return controller; }, surfaceFactory: (BuildContext context, PlatformViewController controller) { + surfaceViewIds.add(controller.viewId); return PlatformViewSurface( gestureRecognizers: const >{}, controller: controller, @@ -2107,6 +2109,13 @@ void main() { ]), ); + expect( + surfaceViewIds, + unorderedEquals([ + currentViewId+1, currentViewId+2, + ]), + ); + expect( viewTypes, unorderedEquals([