Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I am trying to load an HTML form in a webview that has some callbacks on button clicks which I am trying to handle in a javascript handler but are not getting invoked.
Library used
flutter_inappwebview: ^5.7.1
This is how I defined the webview.
InAppWebView(
initialOptions: InAppWebViewGroupOptions(
crossPlatform: InAppWebViewOptions(
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: false,
android: AndroidInAppWebViewOptions(
useHybridComposition: true,
ios: IOSInAppWebViewOptions(
allowsInlineMediaPlayback: true,
initialUrlRequest: URLRequest(
url: Uri.dataFromString(
'about:blank',
mimeType: 'text/html',
onWebViewCreated: (controller) {
controller.addJavaScriptHandler(
handlerName: 'showToast',
callback: (data) {
AppUtils.showMessage(data);
print('data -$data');
setState((){
_controller = controller;
loadForm();
onLoadStop: (controller,uri){
onConsoleMessage: (controller, message) {
print('on Console message - $message');
And in load form I am using an html page which looks like
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<script>
isAppReady = false;
window.addEventListener("flutterInAppWebViewPlatformReady", function (event) {
isAppReady = true;
function buttonClick() {
if (isAppReady) {
window.flutter_inappwebview.callHandler('showToast', 'result');
</script>
<button onclick="buttonClick()" type="button">Show</button>
</body>
</html>
Now The event listener is never invoked hence the boolean value is app ready and is not changed. Any help regarding why it is not getting invoked is appreciated
Here is a simple working example using the current latest plugin version 6 (6.0.0-beta.17):
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
if (!kIsWeb &&
kDebugMode &&
defaultTargetPlatform == TargetPlatform.android) {
await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
runApp(const MaterialApp(home: MyApp()));
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
class _MyAppState extends State<MyApp> {
final GlobalKey webViewKey = GlobalKey();
InAppWebViewController? webViewController;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("InAppWebView test"),
body: Column(children: <Widget>[
Expanded(
child: InAppWebView(
key: webViewKey,
initialData: InAppWebViewInitialData(data: """
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
</head>
<script>
isAppReady = false;
window.addEventListener("flutterInAppWebViewPlatformReady", function (event) {
isAppReady = true;
function buttonClick() {
if (isAppReady) {
window.flutter_inappwebview.callHandler('showToast', 'result');
</script>
<button onclick="buttonClick()" type="button">Show</button>
</body>
</html>
"""),
onWebViewCreated: (controller) {
webViewController = controller;
controller.addJavaScriptHandler(
handlerName: 'showToast',
callback: (arguments) {
final String data = arguments[0];
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(data),
duration: const Duration(seconds: 1),
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.