I'm encountering the following error when using the webview_flutter package in Flutter: Could not cast value of type NSNull (0x1ec725448) to <'webview_flutter_wkwebview.AuthenticationChallengeResponse'>(0x10591eef8)
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class PaymentWebview extends StatefulWidget {
final String initialUrl;
const PaymentWebview({super.key, required this.initialUrl});
@override
State<PaymentWebview> createState() => _PaymentWebviewState();
}
class _PaymentWebviewState extends State<PaymentWebview> {
late final WebViewController _controller;
@override
void initState() {
super.initState();
// Инициализация WebView
try {
late final PlatformWebViewControllerCreationParams params;
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
params = WebKitWebViewControllerCreationParams(
allowsInlineMediaPlayback: true,
mediaTypesRequiringUserAction: const <PlaybackMediaTypes>{},
);
} else {
params = const PlatformWebViewControllerCreationParams();
}
final WebViewController controller =
WebViewController.fromPlatformCreationParams(params);
controller
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
debugPrint('Загрузка: $progress%');
},
onPageStarted: (String url) {
debugPrint('Страница начала загружаться: $url');
},
onPageFinished: (String url) {
if (!mounted) return; // Проверяем, что экран не был закрыт
if (url.contains("success")) {
print("✅ Оплата успешна: $url");
if (mounted) {
Navigator.pop(context, true);
}
} else if (url.contains("fail")) {
print("❌ Ошибка оплаты: $url");
if (mounted) Navigator.pop(context, false);
}
},
onHttpError: (HttpResponseError error) {
debugPrint('HttpResponseError: ${error.response}');
},
onWebResourceError: (WebResourceError error) {
debugPrint('WebResourceError: ${error.description}');
},
onHttpAuthRequest: null,
onNavigationRequest: (NavigationRequest request) {
return NavigationDecision.navigate;
},
),
)
..loadRequest(Uri.parse(widget.initialUrl));
_controller = controller;
} catch (ex) {
print('Error in webview: $ex');
}
}
@override
void dispose() {
if (mounted) {
_controller.clearCache();
}
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text("Payment")),
body: Stack(
children: [
WebViewWidget(controller: _controller),
],
),
);
}
}
Error: Could not cast value of type NSNull (0x1ec725448) to 'webview_flutter_wkwebview.AuthenticationChallengeResponse' (0x10591eef8)
Context:
1.This error occurs when loading the WebView. 2.The URL provided to loadRequest works fine in a browser, but the WebView throws this error when interacting with certain parts of the page. 3.I have implemented a NavigationDelegate to handle page loading, JavaScript, and error handling.
This is caused by a bug with recent update of webview_flutter_wkwebview
(3.18.0), as discussed in this issue on Flutter repo.
You can temporarily fix it by overriding dependency version in pubspec.yaml
back to 3.17.0:
dependency_overrides:
webview_flutter_wkwebview: 3.17.0