From f9a76aea871230e2962856d740385f6f9a297862 Mon Sep 17 00:00:00 2001 From: David Iglesias Date: Thu, 26 Sep 2024 17:55:31 -0700 Subject: [PATCH] Throw StateError when implicitView is null on `wrapWithDefaultView`. (#155734) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR tweaks `wrapWithDefaultView` (used by `runApp`) to raise a StateError with a legible error message when the `platformDispatcher.implicitView` is missing (for example, when enabling multi-view embedding on the web), instead of crashing with an unexpected `nullCheck` a few lines below. * Before: Screenshot 2024-09-25 at 7 33 47 PM * After: Screenshot 2024-09-26 at 5 01 49 PM ## Issues * Fixes https://github.com/flutter/flutter/issues/153198 ## Tests Added a test to ensure the assertion is thrown when the `implicitView` is missing. Had to hack a little because I couldn't find any clean way of overriding the `implicitView`. The problem is that the flutter_test bindings [use `runApp` internally](https://github.com/flutter/flutter/blob/8925e1ffdfe880b06a8bc5877f017083d6652f5b/packages/flutter_test/lib/src/binding.dart#L1020) a couple of times, so I can only disable the implicitView inside the test body (and must re-enable it before returning). Not sure if it's the best way, but it seems to do the trick for this simple test case! --- packages/flutter/lib/src/widgets/binding.dart | 17 +++++++ ...ti_view_no_implicit_view_binding_test.dart | 44 ++++++++++++++++++ .../test/widgets/multi_view_testing.dart | 45 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 packages/flutter/test/widgets/multi_view_no_implicit_view_binding_test.dart diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index 2d47393a28..548ab2a799 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -1225,10 +1225,27 @@ mixin WidgetsBinding on BindingBase, ServicesBinding, SchedulerBinding, GestureB /// /// The [View] determines into what [FlutterView] the app is rendered into. /// This is currently [PlatformDispatcher.implicitView] from [platformDispatcher]. + /// This method will throw a [StateError] if the [PlatformDispatcher.implicitView] + /// is null. /// /// The `rootWidget` widget provided to this method must not already be /// wrapped in a [View]. Widget wrapWithDefaultView(Widget rootWidget) { + if (platformDispatcher.implicitView == null) { + throw StateError( + 'The app requested a view, but the platform did not provide one.\n' + 'This is likely because the app called `runApp` to render its root ' + 'widget, which expects the platform to provide a default view to ' + 'render into (the "implicit" view).\n' + 'However, the platform likely has multi-view mode enabled, which does ' + 'not create this default "implicit" view.\n' + 'Try using `runWidget` instead of `runApp` to start your app.\n' + '`runWidget` allows you to provide a `View` widget, without requiring ' + 'a default view.' + '${kIsWeb?"\nSee: https://flutter.dev/to/web-multiview-runwidget" : ""}' + ); + } + return View( view: platformDispatcher.implicitView!, deprecatedDoNotUseWillBeRemovedWithoutNoticePipelineOwner: pipelineOwner, diff --git a/packages/flutter/test/widgets/multi_view_no_implicit_view_binding_test.dart b/packages/flutter/test/widgets/multi_view_no_implicit_view_binding_test.dart new file mode 100644 index 0000000000..7dd840de3d --- /dev/null +++ b/packages/flutter/test/widgets/multi_view_no_implicit_view_binding_test.dart @@ -0,0 +1,44 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import 'multi_view_testing.dart'; + +void main() { + // Overrides the default test bindings so we have the ability to hide the + // implicitView inside the body of a test. + final NoImplicitViewWidgetsBinding binding = NoImplicitViewWidgetsBinding(); + + testWidgets('NoImplicitViewWidgetsBinding self-test', (WidgetTester tester) async { + expect(tester.platformDispatcher.implicitView, isNotNull); + + // Hide the implicitView from the test harness. + binding.hideImplicitView(); + + expect(tester.platformDispatcher.implicitView, isNull); + + // Ensure the test harness finds the implicitView. + binding.showImplicitView(); + }); + + testWidgets('null implicitView - runApp throws assertion, suggests to use `runWidget`.', (WidgetTester tester) async { + // Hide the implicitView from the test harness. + binding.hideImplicitView(); + + expect(() { + runApp(Container()); + }, throwsA( + isA().having( + (StateError error) => error.message, + 'description', + contains('Try using `runWidget` instead of `runApp`'), ), + ), + ); + + // Ensure the test harness finds the implicitView. + binding.showImplicitView(); + }); +} diff --git a/packages/flutter/test/widgets/multi_view_testing.dart b/packages/flutter/test/widgets/multi_view_testing.dart index 5de23630e5..08dfcea30c 100644 --- a/packages/flutter/test/widgets/multi_view_testing.dart +++ b/packages/flutter/test/widgets/multi_view_testing.dart @@ -31,3 +31,48 @@ class FakeView extends TestFlutterView { // not produce consistent semantics trees. } } + +/// A test platform dispatcher that can show/hide its underlying `implicitView`, +/// depending on the value of the [implicitViewHidden] flag. +class NoImplicitViewPlatformDispatcher extends TestPlatformDispatcher { + NoImplicitViewPlatformDispatcher({ required super.platformDispatcher }) : superPlatformDispatcher = platformDispatcher; + + final PlatformDispatcher superPlatformDispatcher; + + bool implicitViewHidden = false; + + @override + TestFlutterView? get implicitView { + return implicitViewHidden + ? null + : superPlatformDispatcher.implicitView as TestFlutterView?; + } +} + +/// Test Flutter Bindings that allow tests to hide/show the `implicitView` +/// of their [NoImplicitViewPlatformDispatcher] `platformDispatcher`. +/// +/// This is used to test that [runApp] throws an assertion error with an +/// explanation when used when the `implicitView` is disabled (like in Flutter +/// web when multi-view is enabled). +/// +/// Because of how [testWidgets] uses `runApp` internally to manage the lifecycle +/// of a test, the implicitView must be disabled/reenabled inside of the body of +/// the [WidgetTesterCallback] under test. In practice: the implicitView is disabled +/// in the first line of the test, and reenabled in the last. +/// +/// See: multi_view_no_implicitView_binding_test.dart +class NoImplicitViewWidgetsBinding extends AutomatedTestWidgetsFlutterBinding { + late final NoImplicitViewPlatformDispatcher _platformDispatcher = NoImplicitViewPlatformDispatcher(platformDispatcher: super.platformDispatcher); + + @override + NoImplicitViewPlatformDispatcher get platformDispatcher => _platformDispatcher; + + void hideImplicitView() { + platformDispatcher.implicitViewHidden = true; + } + + void showImplicitView() { + platformDispatcher.implicitViewHidden = false; + } +}