Skip to content

Commit a47e719

Browse files
[webview_flutter_platform_interface] Improves error message when WebViewPlatform.instance is null (flutter#6938)
* add assertion * formatting
1 parent d485c7e commit a47e719

File tree

7 files changed

+96
-1
lines changed

7 files changed

+96
-1
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.1
2+
3+
* Improves error message when a platform interface class is used before `WebViewPlatform.instance` has been set.
4+
15
## 2.0.0
26

37
* **Breaking Change**: Releases new interface. See [documentation](https://pub.dev/documentation/webview_flutter_platform_interface/2.0.0/) and [design doc](https://flutter.dev/go/webview_flutter_4_interface)

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_navigation_delegate.dart

+7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
3131
/// Creates a new [PlatformNavigationDelegate]
3232
factory PlatformNavigationDelegate(
3333
PlatformNavigationDelegateCreationParams params) {
34+
assert(
35+
WebViewPlatform.instance != null,
36+
'A platform implementation for `webview_flutter` has not been set. Please '
37+
'ensure that an implementation of `WebViewPlatform` has been set to '
38+
'`WebViewPlatform.instance` before use. For unit testing, '
39+
'`WebViewPlatform.instance` can be set with your own test implementation.',
40+
);
3441
final PlatformNavigationDelegate callbackDelegate =
3542
WebViewPlatform.instance!.createPlatformNavigationDelegate(params);
3643
PlatformInterface.verify(callbackDelegate, _token);

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ abstract class PlatformWebViewController extends PlatformInterface {
2121
/// Creates a new [PlatformWebViewController]
2222
factory PlatformWebViewController(
2323
PlatformWebViewControllerCreationParams params) {
24+
assert(
25+
WebViewPlatform.instance != null,
26+
'A platform implementation for `webview_flutter` has not been set. Please '
27+
'ensure that an implementation of `WebViewPlatform` has been set to '
28+
'`WebViewPlatform.instance` before use. For unit testing, '
29+
'`WebViewPlatform.instance` can be set with your own test implementation.',
30+
);
2431
final PlatformWebViewController webViewControllerDelegate =
2532
WebViewPlatform.instance!.createPlatformWebViewController(params);
2633
PlatformInterface.verify(webViewControllerDelegate, _token);

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_cookie_manager.dart

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
1919
/// Creates a new [PlatformWebViewCookieManager]
2020
factory PlatformWebViewCookieManager(
2121
PlatformWebViewCookieManagerCreationParams params) {
22+
assert(
23+
WebViewPlatform.instance != null,
24+
'A platform implementation for `webview_flutter` has not been set. Please '
25+
'ensure that an implementation of `WebViewPlatform` has been set to '
26+
'`WebViewPlatform.instance` before use. For unit testing, '
27+
'`WebViewPlatform.instance` can be set with your own test implementation.',
28+
);
2229
final PlatformWebViewCookieManager cookieManagerDelegate =
2330
WebViewPlatform.instance!.createPlatformCookieManager(params);
2431
PlatformInterface.verify(cookieManagerDelegate, _token);

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_widget.dart

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import 'webview_platform.dart';
1111
abstract class PlatformWebViewWidget extends PlatformInterface {
1212
/// Creates a new [PlatformWebViewWidget]
1313
factory PlatformWebViewWidget(PlatformWebViewWidgetCreationParams params) {
14+
assert(
15+
WebViewPlatform.instance != null,
16+
'A platform implementation for `webview_flutter` has not been set. Please '
17+
'ensure that an implementation of `WebViewPlatform` has been set to '
18+
'`WebViewPlatform.instance` before use. For unit testing, '
19+
'`WebViewPlatform.instance` can be set with your own test implementation.',
20+
);
1421
final PlatformWebViewWidget webViewWidgetDelegate =
1522
WebViewPlatform.instance!.createPlatformWebViewWidget(params);
1623
PlatformInterface.verify(webViewWidgetDelegate, _token);

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutte
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.0.0
7+
version: 2.0.1
88

99
environment:
1010
sdk: ">=2.12.0 <3.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/webview_platform_test.dart

+63
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,69 @@ void main() {
1818
expect(WebViewPlatform.instance, isNull);
1919
});
2020

21+
// This test can only run while `WebViewPlatform.instance` is still null.
22+
test(
23+
'Interface classes throw assertion error when `WebViewPlatform.instance` is null',
24+
() {
25+
expect(
26+
() => PlatformNavigationDelegate(
27+
const PlatformNavigationDelegateCreationParams(),
28+
),
29+
throwsA(isA<AssertionError>().having(
30+
(AssertionError error) => error.message,
31+
'message',
32+
'A platform implementation for `webview_flutter` has not been set. Please '
33+
'ensure that an implementation of `WebViewPlatform` has been set to '
34+
'`WebViewPlatform.instance` before use. For unit testing, '
35+
'`WebViewPlatform.instance` can be set with your own test implementation.',
36+
)),
37+
);
38+
39+
expect(
40+
() => PlatformWebViewController(
41+
const PlatformWebViewControllerCreationParams(),
42+
),
43+
throwsA(isA<AssertionError>().having(
44+
(AssertionError error) => error.message,
45+
'message',
46+
'A platform implementation for `webview_flutter` has not been set. Please '
47+
'ensure that an implementation of `WebViewPlatform` has been set to '
48+
'`WebViewPlatform.instance` before use. For unit testing, '
49+
'`WebViewPlatform.instance` can be set with your own test implementation.',
50+
)),
51+
);
52+
53+
expect(
54+
() => PlatformWebViewCookieManager(
55+
const PlatformWebViewCookieManagerCreationParams(),
56+
),
57+
throwsA(isA<AssertionError>().having(
58+
(AssertionError error) => error.message,
59+
'message',
60+
'A platform implementation for `webview_flutter` has not been set. Please '
61+
'ensure that an implementation of `WebViewPlatform` has been set to '
62+
'`WebViewPlatform.instance` before use. For unit testing, '
63+
'`WebViewPlatform.instance` can be set with your own test implementation.',
64+
)),
65+
);
66+
67+
expect(
68+
() => PlatformWebViewWidget(
69+
PlatformWebViewWidgetCreationParams(
70+
controller: MockWebViewControllerDelegate(),
71+
),
72+
),
73+
throwsA(isA<AssertionError>().having(
74+
(AssertionError error) => error.message,
75+
'message',
76+
'A platform implementation for `webview_flutter` has not been set. Please '
77+
'ensure that an implementation of `WebViewPlatform` has been set to '
78+
'`WebViewPlatform.instance` before use. For unit testing, '
79+
'`WebViewPlatform.instance` can be set with your own test implementation.',
80+
)),
81+
);
82+
});
83+
2184
test('Cannot be implemented with `implements`', () {
2285
expect(() {
2386
WebViewPlatform.instance = ImplementsWebViewPlatform();

0 commit comments

Comments
 (0)