Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView in Flutter Web

I am trying to display a web view in Flutter for Web but I got the following error :

PlatformException(Unregistered factory, No factory registered for viewtype 'plugins.flutter.io/webview', null)

Is there a way to display a WebView in Flutter Web ?

like image 921
Pyth0nGh057 Avatar asked Sep 28 '19 21:09

Pyth0nGh057


People also ask

What is WebView in Flutter?

With the WebView Flutter plugin you can add a WebView widget to your Android or iOS Flutter app. On iOS the WebView widget is backed by a WKWebView, while on Android the WebView widget is backed by a WebView. The plugin can render Flutter widgets over the web view.


2 Answers

EDIT: Here is a runnable example as of May 16, 2021:

import 'dart:html';
import 'package:flutter/material.dart';
import 'dart:ui' as ui;

void main() {
  // ignore: undefined_prefixed_name
  ui.platformViewRegistry.registerViewFactory(
      'hello-world-html',
          (int viewId) => IFrameElement()
        ..width = '640'
        ..height = '360'
        ..src = 'https://www.youtube.com/embed/IyFZznAk69U'
        ..style.border = 'none');

  runApp(Directionality(
    textDirection: TextDirection.ltr,
    child: SizedBox(
      width: 640,
      height: 360,
      child: HtmlElementView(viewType: 'hello-world-html'),
    ),
  ));
}

The remainder of this post just replicates what's above, and is the original post by the original author

You need at first perform platformViewRegistry:

  ui.platformViewRegistry.registerViewFactory(
  'hello-world-html',
  (int viewId) => IFrameElement()
    ..width = '640'
    ..height = '360'
    ..src = 'https://www.youtube.com/embed/IyFZznAk69U'
    ..style.border = 'none');

Look at that example. In that example old library was imported (on 29.09.19), but if you change 'flutter_web' on 'flutter' it have to work.

Also, you can use not only 'IFrameElement', it can be regular html:

    ui.platformViewRegistry.registerViewFactory("simple_div", (int viewId) {
  DivElement element = DivElement();
  ...
  return element;
like image 78
alex89607 Avatar answered Oct 25 '22 23:10

alex89607


You can try using the easy_web_view plugin.

For iOS and Android, it uses the native webview_flutter plugin and for the Web, it does similar things from the @alex89607 answer.

like image 40
Alex Avatar answered Oct 25 '22 23:10

Alex