Skip to content

Commit 1650c9b

Browse files
committed
Resolved PR comments
1 parent 32b6d05 commit 1650c9b

File tree

14 files changed

+113
-88
lines changed

14 files changed

+113
-88
lines changed

packages/webview_flutter/webview_flutter/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## 4.14.0
22

3-
* Deprecates `WebViewController.loadFile(String)` in favor of `WebViewController.loadFileWithParams(LoadFileParams)`.
43
* Introduces `AndroidLoadFileParams` and `WebKitLoadFileParams` to support platform-specific parameters when loading local HTML files on Android and iOS/macOS.
54

65
## 4.13.0

packages/webview_flutter/webview_flutter/lib/src/webview_controller.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,10 @@ class WebViewController {
131131
/// `/Users/username/Documents/www/index.html`.
132132
///
133133
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
134-
@Deprecated('Use loadFileWithParams(LocalFileParams params) instead. '
135-
'This method will be removed in a future release.')
136134
Future<void> loadFile(String absoluteFilePath) {
137135
return platform.loadFile(absoluteFilePath);
138136
}
139137

140-
/// Loads a local HTML file using the provided [params].
141-
///
142-
/// The [params] object should contain the absolute path to the
143-
/// file as it is stored on the device. For example:
144-
/// `/Users/username/Documents/www/index.html`.
145-
/// In addition, it may include platform-specific fields,
146-
/// such as headers (on Android) or resource paths (on iOS/macOS).
147-
///
148-
/// Throws a `PlatformException` if the [absoluteFilePath] does not exist.
149-
Future<void> loadFileWithParams(LoadFileParams params) {
150-
return platform.loadFileWithParams(params);
151-
}
152-
153138
/// Loads the Flutter asset specified in the pubspec.yaml file.
154139
///
155140
/// Throws a `PlatformException` if [key] is not part of the specified assets

packages/webview_flutter/webview_flutter/test/webview_controller_test.dart

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ void main() {
2727
verify(mockPlatformWebViewController.loadFile('file/path'));
2828
});
2929

30-
test('loadFileWithParams', () async {
31-
final MockPlatformWebViewController mockPlatformWebViewController =
32-
MockPlatformWebViewController();
33-
34-
final WebViewController webViewController = WebViewController.fromPlatform(
35-
mockPlatformWebViewController,
36-
);
37-
38-
await webViewController.loadFileWithParams(
39-
const LoadFileParams(absoluteFilePath: 'file/path'),
40-
);
41-
verify(mockPlatformWebViewController.loadFileWithParams(
42-
const LoadFileParams(absoluteFilePath: 'file/path'),
43-
));
44-
});
45-
4630
test('loadFlutterAsset', () async {
4731
final MockPlatformWebViewController mockPlatformWebViewController =
4832
MockPlatformWebViewController();

packages/webview_flutter/webview_flutter/test/webview_controller_test.mocks.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ class MockPlatformWebViewController extends _i1.Mock
7878
returnValueForMissingStub: _i5.Future<void>.value(),
7979
) as _i5.Future<void>);
8080

81-
@override
82-
_i5.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
83-
(super.noSuchMethod(
84-
Invocation.method(#loadFileWithParams, [params]),
85-
returnValue: _i5.Future<void>.value(),
86-
returnValueForMissingStub: _i5.Future<void>.value(),
87-
) as _i5.Future<void>);
88-
8981
@override
9082
_i5.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
9183
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter/test/webview_widget_test.mocks.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,6 @@ class MockPlatformWebViewController extends _i1.Mock
9191
returnValueForMissingStub: _i7.Future<void>.value(),
9292
) as _i7.Future<void>);
9393

94-
@override
95-
_i7.Future<void> loadFileWithParams(_i2.LoadFileParams? params) =>
96-
(super.noSuchMethod(
97-
Invocation.method(#loadFileWithParams, [params]),
98-
returnValue: _i7.Future<void>.value(),
99-
returnValueForMissingStub: _i7.Future<void>.value(),
100-
) as _i7.Future<void>);
101-
10294
@override
10395
_i7.Future<void> loadFlutterAsset(String? key) => (super.noSuchMethod(
10496
Invocation.method(#loadFlutterAsset, [key]),

packages/webview_flutter/webview_flutter_android/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 4.8.0
22

3+
* Adds support for `PlatformWebViewController.loadFileWithParams`.
34
* Introduces `AndroidLoadFileParams`, a platform-specific extension of `LoadFileParams` for Android that adds support for `headers`.
45

56
## 4.7.0

packages/webview_flutter/webview_flutter_android/lib/src/android_webview_controller.dart

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ import 'weak_reference_utils.dart';
2323
@immutable
2424
class AndroidLoadFileParams extends LoadFileParams {
2525
/// Constructs a [AndroidLoadFileParams], the subclass of a [LoadFileParams].
26-
///
27-
/// Optionally, [headers] map may be included, which contains key-value pairs
28-
/// that will be passed as additional HTTP headers when loading the file.
2926
AndroidLoadFileParams({
3027
required String absoluteFilePath,
31-
this.headers = _defaultHeaders,
28+
this.headers = const <String, String>{},
3229
}) : super(
3330
absoluteFilePath: absoluteFilePath.startsWith('file://')
3431
? absoluteFilePath
@@ -38,22 +35,18 @@ class AndroidLoadFileParams extends LoadFileParams {
3835
/// Constructs a [AndroidLoadFileParams] using a [LoadFileParams].
3936
factory AndroidLoadFileParams.fromLoadFileParams(
4037
LoadFileParams params, {
41-
Map<String, String> headers = _defaultHeaders,
38+
Map<String, String> headers = const <String, String>{},
4239
}) {
4340
return AndroidLoadFileParams(
4441
absoluteFilePath: params.absoluteFilePath,
4542
headers: headers,
4643
);
4744
}
4845

49-
/// Default empty headers used when no custom headers are provided.
50-
///
51-
/// This constant ensures that the [headers] parameter is never `null`,
52-
/// simplifying internal logic and avoiding the need for null checks.
53-
static const Map<String, String> _defaultHeaders = <String, String>{};
54-
5546
/// Additional HTTP headers to be included when loading the local file.
5647
///
48+
/// If not provided at initialization time, doesn't add any addition headers.
49+
///
5750
/// On Android, WebView supports adding headers when loading local or remote
5851
/// content. This can be useful for scenarios like authentication,
5952
/// content-type overrides, or custom request context.
@@ -425,18 +418,31 @@ class AndroidWebViewController extends PlatformWebViewController {
425418
_webView.pigeon_instanceManager.getIdentifier(_webView)!;
426419

427420
@override
428-
Future<void> loadFileWithParams(LoadFileParams params) {
421+
Future<void> loadFile(
422+
String absoluteFilePath,
423+
) {
424+
return loadFileWithParams(
425+
AndroidLoadFileParams(
426+
absoluteFilePath: absoluteFilePath,
427+
),
428+
);
429+
}
430+
431+
@override
432+
Future<void> loadFileWithParams(
433+
LoadFileParams params,
434+
) async {
429435
switch (params) {
430436
case final AndroidLoadFileParams params:
431-
_webView.settings.setAllowFileAccess(true);
432-
return _webView.loadUrl(
433-
params.absoluteFilePath,
434-
params.headers,
435-
);
437+
await Future.wait(<Future<void>>[
438+
_webView.settings.setAllowFileAccess(true),
439+
_webView.loadUrl(params.absoluteFilePath, params.headers),
440+
]);
436441

437442
default:
438-
return loadFileWithParams(
439-
AndroidLoadFileParams.fromLoadFileParams(params));
443+
await loadFileWithParams(
444+
AndroidLoadFileParams.fromLoadFileParams(params),
445+
);
440446
}
441447
}
442448

packages/webview_flutter/webview_flutter_android/test/android_webview_controller_test.dart

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,60 @@ void main() {
292292
verify(mockWebSettings.setUseWideViewPort(false)).called(1);
293293
});
294294

295+
group('loadFile', () {
296+
test('Without file prefix', () async {
297+
final MockWebView mockWebView = MockWebView();
298+
final MockWebSettings mockWebSettings = MockWebSettings();
299+
final AndroidWebViewController controller = createControllerWithMocks(
300+
mockWebView: mockWebView,
301+
mockSettings: mockWebSettings,
302+
);
303+
304+
await controller.loadFile('/path/to/file.html');
305+
306+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
307+
verify(mockWebView.loadUrl(
308+
'file:///path/to/file.html',
309+
<String, String>{},
310+
)).called(1);
311+
});
312+
313+
test('Without file prefix and characters to be escaped', () async {
314+
final MockWebView mockWebView = MockWebView();
315+
final MockWebSettings mockWebSettings = MockWebSettings();
316+
final AndroidWebViewController controller = createControllerWithMocks(
317+
mockWebView: mockWebView,
318+
mockSettings: mockWebSettings,
319+
);
320+
321+
await controller.loadFile('/path/to/?_<_>_.html');
322+
323+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
324+
verify(mockWebView.loadUrl(
325+
'file:///path/to/%3F_%3C_%3E_.html',
326+
<String, String>{},
327+
)).called(1);
328+
});
329+
330+
test('With file prefix', () async {
331+
final MockWebView mockWebView = MockWebView();
332+
final MockWebSettings mockWebSettings = MockWebSettings();
333+
final AndroidWebViewController controller = createControllerWithMocks(
334+
mockWebView: mockWebView,
335+
);
336+
337+
when(mockWebView.settings).thenReturn(mockWebSettings);
338+
339+
await controller.loadFile('file:///path/to/file.html');
340+
341+
verify(mockWebSettings.setAllowFileAccess(true)).called(1);
342+
verify(mockWebView.loadUrl(
343+
'file:///path/to/file.html',
344+
<String, String>{},
345+
)).called(1);
346+
});
347+
});
348+
295349
group('loadFileWithParams', () {
296350
group('Using LoadFileParams model', () {
297351
test('Without file prefix', () async {

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
## 2.14.0
22

3-
* Updates minimum supported SDK version to Flutter 3.27/Dart 3.6.
43
* Adds `PlatformWebViewController.loadFileWithParams(LoadFileParams)` to support loading local HTML files with platform-specific parameters.
5-
* Deprecates `PlatformWebViewController.loadFile(String)` in favour of `PlatformWebViewController.loadFileWithParams(LoadFileParams)`.
64

75
## 2.13.1
86

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,11 @@ abstract class PlatformWebViewController extends PlatformInterface {
5454
/// `/Users/username/Documents/www/index.html`.
5555
///
5656
/// Throws an ArgumentError if the [absoluteFilePath] does not exist.
57-
@Deprecated('Use loadFileWithParams(LocalFileParams params) instead. '
58-
'This method will be removed in a future release.')
5957
Future<void> loadFile(
6058
String absoluteFilePath,
6159
) {
62-
return loadFileWithParams(
63-
LoadFileParams(absoluteFilePath: absoluteFilePath));
60+
throw UnimplementedError(
61+
'loadFile is not implemented on the current platform');
6462
}
6563

6664
/// Loads a local HTML file using the provided [params].
@@ -71,9 +69,6 @@ abstract class PlatformWebViewController extends PlatformInterface {
7169
///
7270
/// Platform-specific implementations may extend [LocalFileParams] to support
7371
/// additional parameters, such as iOS/macOS-specific read access options.
74-
///
75-
/// Throws an [ArgumentError] if the file specified in
76-
/// [params.absoluteFilePath] does not exist.
7772
Future<void> loadFileWithParams(
7873
LoadFileParams params,
7974
) {

0 commit comments

Comments
 (0)