Skip to content

WebView not working on secondary screen #1

@kerberjg

Description

@kerberjg

I'm trying to display a WebViewWidget (using webview_flutter) inside of the secondary display, but there seems to be some sort of misconfiguration when starting the secondary FlutterEngine/FlutterViewController, as it doesn't have access to the platform channel for the webview plugin.

Error logs:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKWebViewConfigurationHostApi.setMediaTypesRequiringUserActionForPlayback"., null, null)
#0      WKWebViewConfigurationHostApi.setMediaTypesRequiringUserActionForPlayback (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:1334:7)
web_kit.g.dart:1334
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKWebViewConfigurationHostApi.setAllowsInlineMediaPlayback"., null, null)
#0      WKWebViewConfigurationHostApi.setAllowsInlineMediaPlayback (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:1284:7)
web_kit.g.dart:1284
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKWebViewHostApi.create"., null, null)
#0      WKWebViewHostApi.create (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:2603:7)
web_kit.g.dart:2603
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.NSObjectHostApi.addObserver"., null, null)
#0      NSObjectHostApi.addObserver (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:2285:7)
web_kit.g.dart:2285
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.NSObjectHostApi.addObserver"., null, null)
#0      NSObjectHostApi.addObserver (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:2285:7)
web_kit.g.dart:2285
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKUIDelegateHostApi.create"., null, null)
#0      WKUIDelegateHostApi.create (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:3124:7)
web_kit.g.dart:3124
<asynchronous suspension>
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKWebViewHostApi.setUIDelegate"., null, null)
#0      WKWebViewHostApi.setUIDelegate (package:webview_flutter_wkwebview/src/common/web_kit.g.dart:2627:7)
web_kit.g.dart:2627
<asynchronous suspension>
flutter: Error loading page: PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.webview_flutter_wkwebview.WKWebViewHostApi.loadRequest"., null, null)
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(unregistered_view_type, A UIKitView widget is trying to create a PlatformView with an unregistered type: < plugins.flutter.io/webview >, If you are the author of the PlatformView, make sure `registerViewFactory` is invoked.
See: https://docs.flutter.dev/development/platform-integration/platform-views#on-the-platform-side-1 for more details.
If you are not the author of the PlatformView, make sure to call `GeneratedPluginRegistrant.register`., null)
#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:648:7)
message_codecs.dart:648
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:334:18)
platform_channel.dart:334
<asynchronous suspension>
#2      PlatformViewsService.initUiKitView (package:flutter/src/services/platform_views.dart:248:5)
platform_views.dart:248
<asynchronous suspension>
#3      _DarwinViewState._createNewUiKitView (package:flutter/src/widgets/platform_view.dart:918:36)
platform_view.dart:918
<asynchronous suspensio<…>

Reproduction code:

import 'dart:math';

import 'package:external_display/external_display.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

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

@pragma('vm:entry-point')
void externalDisplayMain() {
  runApp(ExternalApp());
}

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

  Future<void> handle(Future future, String name) async {
    try {
      final result = await future;
      print("Result of $name: $result");
    } catch (e) {
      print("Error in $name: $e");
    }
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    ExternalDisplay externalDisplay = ExternalDisplay();

    externalDisplay.addListener((status) {
      print("External display status: $status");
    });

    return CupertinoApp(
      title: 'Flutter Demo',
      // home: BrowserView(),
      home: CupertinoPageScaffold(child: Center(
        child:Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("Hello Screen ${View.of(context).display.id}", style: CupertinoTheme.of(context).textTheme.textStyle.copyWith(fontWeight: FontWeight.bold),),
            Text("isPlugging: ${externalDisplay.isPlugging}"),
        CupertinoButton.filled(
          child: Text("Connect display"),
          onPressed: () async => handle(externalDisplay.connect(), "connect"),
        ),
        CupertinoButton.filled(
          child: Text("Send message"),
          onPressed: () async => handle(externalDisplay.transferParameters(action: "action", value: DateTime.now().toIso8601String()), "send"),
        ),
      ],)))
    );
  }
}

class ExternalApp extends StatelessWidget {
  ExternalApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return CupertinoApp(
      title: 'Flutter Demo',
      // home: TestView(name: "ext"),
      home: BrowserView(),
    );
  }
}

class TestView extends StatelessWidget {
  final String name;
  TestView({super.key, required this.name});

  @override
  Widget build(BuildContext context) {
    final id = View.of(context).display.id;

    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text((id == 0 ? "Phone Screen" : "External Screen ${id}") + " - ${name}"),
      ),
      child: Center(child: Text("Hello Screen no. ${id}"),)
    );
  }
}

class BrowserView extends StatefulWidget {
  BrowserView({super.key});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the v alues (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final WebViewController controller = WebViewController();

  @override
  State<BrowserView> createState() => _BrowserViewState();
}

class _BrowserViewState extends State<BrowserView> {


  @override
  Widget build(BuildContext context) {
    widget.controller.loadRequest(Uri.parse("https://www.google.com")).then((_) {
      print("Page loaded");
    }, onError: (error) {
      print("Error loading page: $error");
    });

    return CupertinoPageScaffold(
      navigationBar: CupertinoNavigationBar(
        middle: Text("your mom"),
      ),
      child: WebViewWidget(controller: widget.controller)
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions