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

Commit e5fc8b5

Browse files
authored
[webview_flutter] Adds the loadFlutterAsset method to the interface. (#4562)
1 parent 509d3e2 commit e5fc8b5

File tree

5 files changed

+142
-2
lines changed

5 files changed

+142
-2
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.8.0
2+
3+
* Adds the `loadFlutterAsset` method to the platform interface.
4+
15
## 1.7.0
26

37
* Add an option to set the background color of the webview.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/method_channel/webview_method_channel.dart

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,31 @@ class MethodChannelWebViewPlatform implements WebViewPlatformController {
8484
@override
8585
Future<void> loadFile(String absoluteFilePath) async {
8686
assert(absoluteFilePath != null);
87-
return _channel.invokeMethod<void>('loadFile', absoluteFilePath);
87+
88+
try {
89+
return await _channel.invokeMethod<void>('loadFile', absoluteFilePath);
90+
} on PlatformException catch (ex) {
91+
if (ex.code == 'loadFile_failed') {
92+
throw ArgumentError(ex.message);
93+
}
94+
95+
rethrow;
96+
}
97+
}
98+
99+
@override
100+
Future<void> loadFlutterAsset(String key) async {
101+
assert(key.isNotEmpty);
102+
103+
try {
104+
return await _channel.invokeMethod<void>('loadFlutterAsset', key);
105+
} on PlatformException catch (ex) {
106+
if (ex.code == 'loadFlutterAsset_invalidKey') {
107+
throw ArgumentError(ex.message);
108+
}
109+
110+
rethrow;
111+
}
88112
}
89113

90114
@override

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_interface/webview_platform_controller.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ abstract class WebViewPlatformController {
3535
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
3636
Future<void> loadFile(
3737
String absoluteFilePath,
38+
) {
39+
throw UnimplementedError(
40+
'WebView loadFile is not implemented on the current platform');
41+
}
42+
43+
/// Loads the Flutter asset specified in the pubspec.yaml file.
44+
///
45+
/// Throws an ArgumentError if [key] is not part of the specified assets
46+
/// in the pubspec.yaml file.
47+
Future<void> loadFlutterAsset(
48+
String key,
3849
) {
3950
throw UnimplementedError(
4051
'WebView loadFlutterAsset is not implemented on the current platform');

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/webview_flut
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: 1.7.0
7+
version: 1.8.0
88

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

packages/webview_flutter/webview_flutter_platform_interface/test/src/method_channel/webview_method_channel_test.dart

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ void main() {
3333
case 'canGoBack':
3434
case 'canGoForward':
3535
return true;
36+
case 'loadFile':
37+
if (methodCall.arguments == 'invalid file') {
38+
throw PlatformException(
39+
code: 'loadFile_failed',
40+
message: 'Failed loading file.',
41+
details: null);
42+
} else if (methodCall.arguments == 'some error') {
43+
throw PlatformException(
44+
code: 'some_error',
45+
message: 'Some error occurred.',
46+
details: null,
47+
);
48+
}
49+
return null;
50+
case 'loadFlutterAsset':
51+
if (methodCall.arguments == 'invalid key') {
52+
throw PlatformException(
53+
code: 'loadFlutterAsset_invalidKey',
54+
message: 'Failed loading asset.',
55+
details: null,
56+
);
57+
} else if (methodCall.arguments == 'some error') {
58+
throw PlatformException(
59+
code: 'some_error',
60+
message: 'Some error occurred.',
61+
details: null,
62+
);
63+
}
64+
return null;
3665
case 'runJavascriptReturningResult':
3766
case 'evaluateJavascript':
3867
return methodCall.arguments as String;
@@ -74,6 +103,78 @@ void main() {
74103
);
75104
});
76105

106+
test('loadFile with invalid file', () async {
107+
expect(
108+
() => webViewPlatform.loadFile('invalid file'),
109+
throwsA(
110+
isA<ArgumentError>().having(
111+
(ArgumentError error) => error.message,
112+
'message',
113+
'Failed loading file.',
114+
),
115+
),
116+
);
117+
});
118+
119+
test('loadFile with some error.', () async {
120+
expect(
121+
() => webViewPlatform.loadFile('some error'),
122+
throwsA(
123+
isA<PlatformException>().having(
124+
(PlatformException error) => error.message,
125+
'message',
126+
'Some error occurred.',
127+
),
128+
),
129+
);
130+
});
131+
132+
test('loadFlutterAsset', () async {
133+
await webViewPlatform.loadFlutterAsset(
134+
'folder/asset.html',
135+
);
136+
137+
expect(
138+
log,
139+
<Matcher>[
140+
isMethodCall(
141+
'loadFlutterAsset',
142+
arguments: 'folder/asset.html',
143+
),
144+
],
145+
);
146+
});
147+
148+
test('loadFlutterAsset with empty key', () async {
149+
expect(() => webViewPlatform.loadFlutterAsset(''), throwsAssertionError);
150+
});
151+
152+
test('loadFlutterAsset with invalid key', () async {
153+
expect(
154+
() => webViewPlatform.loadFlutterAsset('invalid key'),
155+
throwsA(
156+
isA<ArgumentError>().having(
157+
(ArgumentError error) => error.message,
158+
'message',
159+
'Failed loading asset.',
160+
),
161+
),
162+
);
163+
});
164+
165+
test('loadFlutterAsset with some error.', () async {
166+
expect(
167+
() => webViewPlatform.loadFlutterAsset('some error'),
168+
throwsA(
169+
isA<PlatformException>().having(
170+
(PlatformException error) => error.message,
171+
'message',
172+
'Some error occurred.',
173+
),
174+
),
175+
);
176+
});
177+
77178
test('loadHtmlString without base URL', () async {
78179
await webViewPlatform.loadHtmlString(
79180
'Test HTML string',

0 commit comments

Comments
 (0)