Throw StateError when implicitView is null on wrapWithDefaultView. (#155734)
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:
<img width="619" alt="Screenshot 2024-09-25 at 7 33 47â¯PM" src="https://github.com/user-attachments/assets/4897dd3c-bdd0-4217-9f23-7eee9fab4999">
* After:
<img width="613" alt="Screenshot 2024-09-26 at 5 01 49â¯PM" src="https://github.com/user-attachments/assets/3febb91d-a8c3-41b6-bf34-c2c8743b637c">
## 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](8925e1ffdf/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!
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<StateError>().having(
|
||||
(StateError error) => error.message,
|
||||
'description',
|
||||
contains('Try using `runWidget` instead of `runApp`'), ),
|
||||
),
|
||||
);
|
||||
|
||||
// Ensure the test harness finds the implicitView.
|
||||
binding.showImplicitView();
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user