Skip to content

[webview_flutter_wkwebview] Crash when restarting in debug mode on iOS – Host platform returned null value for non-null return value #168306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ljanecek opened this issue May 5, 2025 · 6 comments · May be fixed by flutter/packages#9252
Assignees
Labels
fyi-ios For the attention of iOS platform team p: webview The WebView plugin P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ecosystem Owned by Ecosystem team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds

Comments

@ljanecek
Copy link

ljanecek commented May 5, 2025

Steps to reproduce

  1. Create a WebViewController instance in initState()
  2. Immediately call setJavaScriptMode(JavaScriptMode.unrestricted)
  3. Run the app on a real device or simulator with iOS 18.5
  4. Hot restart or restart in debug mode
  5. Observe the crash

Expected results

No crash occurs. The WebViewController should safely initialize and configure JavaScript mode, even if the widget is still being built.

Actual results

PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

WKWebViewConfiguration.getDefaultWebpagePreferences (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:3611:7)

WebKitWebViewController.setJavaScriptMode (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:598:11)

Code sample

Code sample
class WebViewScreen extends StatefulWidget {
  const WebViewScreen({super.key, required this.initialUrl});
  final String initialUrl;

  @override
  State<WebViewScreen> createState() => _WebViewScreenState();
}

class _WebViewScreenState extends State<WebViewScreen> with WidgetsBindingObserver {
  late final WebViewController controller;

  @override
  void initState() {
    super.initState();

    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted) // ← causes crash on iOS in debug
      ..setUserAgent('Custom UA');

    controller.loadRequest(
      Uri.parse(widget.initialUrl),
      headers: <String, String>{
        'Authorization': 'External <access_token>',
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebViewWidget(controller: controller),
    );
  }
}

Screenshots or Video

No response

Logs

Logs
PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

WKWebViewConfiguration.getDefaultWebpagePreferences (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:3611:7)

WebKitWebViewController.setJavaScriptMode (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:598:11)

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.3, on macOS 15.4.1 24E263 darwin-arm64, locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.3)
[✓] VS Code (version 1.99.3)
[✓] Connected device (4 available)
[✓] Network resources
@ljanecek ljanecek changed the title ] Crash when restarting in debug mode on iOS – Host platform returned null value for non-null return value [webview_flutter_wkwebview] Crash when restarting in debug mode on iOS – Host platform returned null value for non-null return value May 5, 2025
@darshankawar darshankawar added the in triage Presently being triaged by the triage team label May 6, 2025
@darshankawar
Copy link
Member

@ljanecek
Although not on iOS 18.5, but I tried on 18.2 simulator in which I didn't observe the reported error.
What version of webview_flutter_wkwebview are you running ?
Not sure if it is related to fixed #162437 that you can check and confirm.
#162437 (comment)

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 6, 2025
@sukhcha-in
Copy link

iOS Version: 18.3.1
webview_flutter: 4.11.0
webview_flutter_wkwebview: 3.20.0

Same issue:
[ERROR:flutter/runtime/dart_vm_initializer.cc(40)] Unhandled Exception: PlatformException(null-error, Host platform returned null value for non-null return value., null, null)
#0 WKWebViewConfiguration.getDefaultWebpagePreferences (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:3611:7)
web_kit.g.dart:3611

#1 WebKitWebViewController.setJavaScriptMode (package:webview_flutter_wkwebview/src/webkit_webview_controller.dart:598:11)

@ljanecek
Copy link
Author

ljanecek commented May 9, 2025

Hi @darshankawar, thank you for your reply.

I'm using:
webview_flutter: "4.11.0"
webview_flutter_wkwebview: version: "3.20.0"

I don’t see any relation to #162437

@github-actions github-actions bot removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 9, 2025
@darshankawar
Copy link
Member

@ljanecek
We tried replicating on latest iOS versions with simulator and device but weren't able to see the reported error. Please try to provide more details and the page you are trying to visit that triggers the crash.

@darshankawar darshankawar added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 9, 2025
@bparrishMines
Copy link
Contributor

It looks like WKWebViewConfiguration.defaultWebpagePreferences is actually an implicitly unwrapped optional. Meaning it shouldn't be null, but there is a chance it could be. This can be fixed by setting the value just in case.

@bparrishMines bparrishMines added p: webview The WebView plugin team-ecosystem Owned by Ecosystem team P2 Important issues not at the top of the work list and removed waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds in triage Presently being triaged by the triage team labels May 19, 2025
@bparrishMines bparrishMines self-assigned this May 19, 2025
@bparrishMines bparrishMines added fyi-ios For the attention of iOS platform team platform-ios iOS applications specifically package flutter/packages repository. See also p: labels. labels May 19, 2025
@bparrishMines
Copy link
Contributor

It looks like WKWebViewConfiguration.defaultWebpagePreferences is actually an implicitly unwrapped optional. Meaning it shouldn't be null, but there is a chance it could be. This can be fixed by setting the value just in case.

Actually I was able to reproduce the error with webview_flutter_wkwebview version 3.21.0, but it no longer happens with 3.22.0. I think this was actually fixed in flutter/packages#9281 because it updated the pigeon code that fixed flutter/packages#9231.

@ljanecek When using webview_flutter_wkwebview: ^3.22.0, are you still able to reprocude the error?

@bparrishMines bparrishMines added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fyi-ios For the attention of iOS platform team p: webview The WebView plugin P2 Important issues not at the top of the work list package flutter/packages repository. See also p: labels. platform-ios iOS applications specifically team-ecosystem Owned by Ecosystem team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds
Projects
None yet
4 participants