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 viewty
you can use this package: https://pub.dev/packages/flutter_webview_plugin
and here is an working example you can use:
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var title = 'some title';
return MaterialApp(
title: title,
home: WebviewScaffold(
url: "yourwebsite.com",
withZoom: false,
withLocalStorage: true,
));
}
}
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;
Answer by @mohamed-salah is helpful, however, I was only getting a loading symbol on my screen. We need put webview
inside WillPopScope
widget. Use the following code to properly load webview
-
In pubspec.yaml
add dependency
flutter_webview_plugin: ^0.3.9+1 // replace with latest version
In StatefulWidget
class, use following code -
class _WebViewLoadingState extends State<Details> {
final _webViewPlugin = FlutterWebviewPlugin();
@override
void initState() {
// TODO: implement initState
super.initState();
// on pressing back button, exiting the screen instead of showing loading symbol
_webViewPlugin.onDestroy.listen((_) {
if (Navigator.canPop(context)) {
// exiting the screen
Navigator.of(context).pop();
}
});
}
@override
Widget build(BuildContext context) {
// WillPopScope will prevent loading
return WillPopScope(
child: WebviewScaffold(
url: "https://www.google.com",
withZoom: false,
withLocalStorage: true,
withJavascript: true,
appCacheEnabled: true,
appBar: AppBar(
title: Text("Browser"),
),
),
onWillPop: () {
return _webViewPlugin.close();
}
);
}
}