Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[webview_flutter_platform_interface] Improves error message when WebViewPlatform.instance is null #6938

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.1

* Improves error message when a platform interface class is used before `WebViewPlatform.instance` has been set.

## 2.0.0

* **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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
/// Creates a new [PlatformNavigationDelegate]
factory PlatformNavigationDelegate(
PlatformNavigationDelegateCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformNavigationDelegate callbackDelegate =
WebViewPlatform.instance!.createPlatformNavigationDelegate(params);
PlatformInterface.verify(callbackDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ abstract class PlatformWebViewController extends PlatformInterface {
/// Creates a new [PlatformWebViewController]
factory PlatformWebViewController(
PlatformWebViewControllerCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewController webViewControllerDelegate =
WebViewPlatform.instance!.createPlatformWebViewController(params);
PlatformInterface.verify(webViewControllerDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ abstract class PlatformWebViewCookieManager extends PlatformInterface {
/// Creates a new [PlatformWebViewCookieManager]
factory PlatformWebViewCookieManager(
PlatformWebViewCookieManagerCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewCookieManager cookieManagerDelegate =
WebViewPlatform.instance!.createPlatformCookieManager(params);
PlatformInterface.verify(cookieManagerDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import 'webview_platform.dart';
abstract class PlatformWebViewWidget extends PlatformInterface {
/// Creates a new [PlatformWebViewWidget]
factory PlatformWebViewWidget(PlatformWebViewWidgetCreationParams params) {
assert(
WebViewPlatform.instance != null,
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
);
final PlatformWebViewWidget webViewWidgetDelegate =
WebViewPlatform.instance!.createPlatformWebViewWidget(params);
PlatformInterface.verify(webViewWidgetDelegate, _token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutte
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.0.0
version: 2.0.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,69 @@ void main() {
expect(WebViewPlatform.instance, isNull);
});

// This test can only run while `WebViewPlatform.instance` is still null.
test(
'Interface classes throw assertion error when `WebViewPlatform.instance` is null',
() {
expect(
() => PlatformNavigationDelegate(
const PlatformNavigationDelegateCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewController(
const PlatformWebViewControllerCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewCookieManager(
const PlatformWebViewCookieManagerCreationParams(),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);

expect(
() => PlatformWebViewWidget(
PlatformWebViewWidgetCreationParams(
controller: MockWebViewControllerDelegate(),
),
),
throwsA(isA<AssertionError>().having(
(AssertionError error) => error.message,
'message',
'A platform implementation for `webview_flutter` has not been set. Please '
'ensure that an implementation of `WebViewPlatform` has been set to '
'`WebViewPlatform.instance` before use. For unit testing, '
'`WebViewPlatform.instance` can be set with your own test implementation.',
)),
);
});

test('Cannot be implemented with `implements`', () {
expect(() {
WebViewPlatform.instance = ImplementsWebViewPlatform();
Expand Down