Skip to content

[WebViewController] ios crash when page send undefined message via javascriptChannel #162352

Closed
flutter/packages
#8776
@MariuszHenn

Description

@MariuszHenn

Steps to reproduce

  1. register javascript channel in webview controller
  2. load page that will eventually send undefined message to this channel

Expected results

onMessageReceived callback should receive instance of JavascriptMessage with 'undefined' message as a string.

Actual results

App crashes with an exception

Code sample

After 5 seconds js will send deadly command to application.

Code sample
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    WebViewController controller = WebViewController()
    ..setJavaScriptMode(JavaScriptMode.unrestricted)
    ..addJavaScriptChannel('Channel', onMessageReceived: (message) {
      print('Received message: ${message.toString()}');
    });

    NavigationDelegate navigationDelegate = NavigationDelegate(
      onPageFinished: (String url) {
        controller.runJavaScript("""
        var i=3;
        setInterval(() => {
          document.body.textContent = 'Crash in ' + i;
          --i;
        }, 1000);
 
        setTimeout(() => {
          window.Channel.postMessage(undefined);
        }, 5000);
        """);
      },
    );

     controller.setNavigationDelegate(navigationDelegate);
     controller.loadHtmlString('<html><body>Test</body></html>');

    return MaterialApp(
      home:  Scaffold(
          appBar: AppBar(title: const Text('60km/h crash test')),
          body: WebViewWidget(controller: controller),
    )
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
Simulator.Screen.Recording.-.iPhone.SE.3rd.generation.-.2025-01-29.at.00.56.08.mp4

Logs

Logs
webview_flutter_wkwebview/ProxyAPIRegistrar.swift:71: Fatal error: webview_flutter_wkwebview.PigeonError: Error returned from calling WKScriptMessageHandler.didReceiveScriptMessage: Optional("Null check operator used on a null value")

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.27.3, on macOS 15.1.1 24B91 darwin-arm64, locale pl-PL)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0-rc4)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.96.4)
[✓] Connected device (5 available)            
[✓] Network resources

• No issues found!

Metadata

Metadata

Assignees

Labels

c: fatal crashCrashes that terminate the processfound in release: 3.27Found to occur in 3.27found in release: 3.29Found to occur in 3.29has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: webviewThe WebView pluginpackageflutter/packages repository. See also p: labels.platform-iosiOS applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-ecosystemOwned by Ecosystem team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions