[webview_flutter_wkwebview] Fixes an exception caused by the `onUrlChange` callback returning a null url (#3848)
While investigating another bug, I realized that loading the initial request with an invalid url, the callback returns a null NSURL.
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md b/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
index c43051a..f310aad 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
+++ b/packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 3.4.2
+
+* Fixes an exception caused by the `onUrlChange` callback passing a null `NSUrl`.
+
## 3.4.1
* Fixes internal type conversion error.
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart
index b7975d5..885573f 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart
+++ b/packages/webview_flutter/webview_flutter_wkwebview/lib/src/webkit_webview_controller.dart
@@ -239,8 +239,8 @@
final UrlChangeCallback? urlChangeCallback =
controller._currentNavigationDelegate?._onUrlChange;
if (urlChangeCallback != null) {
- final NSUrl url = change[NSKeyValueChangeKey.newValue]! as NSUrl;
- urlChangeCallback(UrlChange(url: await url.getAbsoluteString()));
+ final NSUrl? url = change[NSKeyValueChangeKey.newValue] as NSUrl?;
+ urlChangeCallback(UrlChange(url: await url?.getAbsoluteString()));
}
break;
}
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/pubspec.yaml b/packages/webview_flutter/webview_flutter_wkwebview/pubspec.yaml
index e1f2e31..55de26f 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/pubspec.yaml
+++ b/packages/webview_flutter/webview_flutter_wkwebview/pubspec.yaml
@@ -2,7 +2,7 @@
description: A Flutter plugin that provides a WebView widget based on Apple's WKWebView control.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter_wkwebview
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
-version: 3.4.1
+version: 3.4.2
environment:
sdk: ">=2.18.0 <4.0.0"
diff --git a/packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_controller_test.dart b/packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_controller_test.dart
index b4a8439..41900b5 100644
--- a/packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_controller_test.dart
+++ b/packages/webview_flutter/webview_flutter_wkwebview/test/webkit_webview_controller_test.dart
@@ -1036,6 +1036,56 @@
expect(urlChange.url, 'https://www.google.com');
});
+ test('setPlatformNavigationDelegate onUrlChange to null NSUrl', () async {
+ final MockWKWebView mockWebView = MockWKWebView();
+
+ late final void Function(
+ String keyPath,
+ NSObject object,
+ Map<NSKeyValueChangeKey, Object?> change,
+ ) webViewObserveValue;
+
+ final WebKitWebViewController controller = createControllerWithMocks(
+ createMockWebView: (
+ _, {
+ void Function(
+ String keyPath,
+ NSObject object,
+ Map<NSKeyValueChangeKey, Object?> change,
+ )? observeValue,
+ }) {
+ webViewObserveValue = observeValue!;
+ return mockWebView;
+ },
+ );
+
+ final WebKitNavigationDelegate navigationDelegate =
+ WebKitNavigationDelegate(
+ const WebKitNavigationDelegateCreationParams(
+ webKitProxy: WebKitProxy(
+ createNavigationDelegate: CapturingNavigationDelegate.new,
+ createUIDelegate: WKUIDelegate.detached,
+ ),
+ ),
+ );
+
+ final Completer<UrlChange> urlChangeCompleter = Completer<UrlChange>();
+ navigationDelegate.setOnUrlChange(
+ (UrlChange change) => urlChangeCompleter.complete(change),
+ );
+
+ await controller.setPlatformNavigationDelegate(navigationDelegate);
+
+ webViewObserveValue(
+ 'URL',
+ mockWebView,
+ <NSKeyValueChangeKey, Object?>{NSKeyValueChangeKey.newValue: null},
+ );
+
+ final UrlChange urlChange = await urlChangeCompleter.future;
+ expect(urlChange.url, isNull);
+ });
+
test('webViewIdentifier', () {
final InstanceManager instanceManager = InstanceManager(
onWeakReferenceRemoved: (_) {},