Skip to content

Commit 2d66f30

Browse files
[webview_flutter_web] Adds auto registration of the WebViewPlatform implementation (#6886)
* Adss auto reg * test for registration * update readme
1 parent 417b370 commit 2d66f30

File tree

5 files changed

+26
-56
lines changed

5 files changed

+26
-56
lines changed

packages/webview_flutter/webview_flutter_web/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.1
2+
3+
* Adds auto registration of the `WebViewPlatform` implementation.
4+
15
## 0.2.0
26

37
* **BREAKING CHANGE** Updates platform implementation to `2.0.0` release of

packages/webview_flutter/webview_flutter_web/README.md

+1-54
Original file line numberDiff line numberDiff line change
@@ -18,59 +18,6 @@ yet, so it currently requires extra setup to use:
1818
* [Add this package](https://pub.dev/packages/webview_flutter_web/install)
1919
as an explicit dependency of your project, in addition to depending on
2020
`webview_flutter`.
21-
* Register `WebWebViewPlatform` as the `WebViewPlatform.instance` before creating a
22-
`WebView`. See below for examples.
2321

24-
Once those steps below are complete, the APIs from `webview_flutter` listed
22+
Once the step above is complete, the APIs from `webview_flutter` listed
2523
above can be used as normal on web.
26-
27-
### Registering the implementation
28-
29-
Before creating a `WebView` (for instance, at the start of `main`), you will
30-
need to register the web implementation.
31-
32-
#### Web-only project example
33-
34-
```dart
35-
...
36-
import 'package:webview_flutter/webview_flutter.dart';
37-
import 'package:webview_flutter_web/webview_flutter_web.dart';
38-
39-
main() {
40-
WebViewPlatform.instance = WebWebViewPlatform();
41-
...
42-
```
43-
44-
#### Multi-platform project example
45-
46-
If your project supports platforms other than web, you will need to use a
47-
conditional import to avoid directly including `webview_flutter_web.dart` on
48-
non-web platforms. For example:
49-
50-
`register_web_webview.dart`:
51-
```dart
52-
import 'package:webview_flutter/webview_flutter.dart';
53-
import 'package:webview_flutter_web/webview_flutter_web.dart';
54-
55-
void registerWebViewWebImplementation() {
56-
WebViewPlatform.instance = WebWebViewPlatform();
57-
}
58-
```
59-
60-
`register_web_webview_stub.dart`:
61-
```dart
62-
void registerWebViewWebImplementation() {
63-
// No-op.
64-
}
65-
```
66-
67-
`main.dart`:
68-
```dart
69-
...
70-
import 'register_web_webview_stub.dart'
71-
if (dart.library.html) 'register_web.dart';
72-
73-
main() {
74-
registerWebViewWebImplementation();
75-
...
76-
```

packages/webview_flutter/webview_flutter_web/lib/src/web_webview_platform.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ class WebWebViewPlatform extends WebViewPlatform {
2424
}
2525

2626
/// Gets called when the plugin is registered.
27-
static void registerWith(Registrar registrar) {}
27+
static void registerWith(Registrar registrar) {
28+
WebViewPlatform.instance = WebWebViewPlatform();
29+
}
2830
}

packages/webview_flutter/webview_flutter_web/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_web
22
description: A Flutter plugin that provides a WebView widget on web.
33
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 0.2.0
5+
version: 0.2.1
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
7+
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
8+
import 'package:webview_flutter_web/webview_flutter_web.dart';
9+
10+
void main() {
11+
group('WebWebViewPlatform', () {
12+
test('registerWith', () {
13+
WebWebViewPlatform.registerWith(Registrar());
14+
expect(WebViewPlatform.instance, isA<WebWebViewPlatform>());
15+
});
16+
});
17+
}

0 commit comments

Comments
 (0)