dart 在flutter_inappwebiew中没有从网站获得响应

chhkpiq4  于 2023-10-13  发布在  Flutter
关注(0)|答案(1)|浏览(123)

我试图从网站上获取响应,因为我使用flutter_inappwebview:^6.0.0-beta.25.在web端我添加了JavaScript代码。但我在扑翼应用里没得到回应如果我调用window.flutter_inappwebview. callback('FlutterFunction',“得到成功消息”);如果没有window. addEventServer,我得到了这个错误“无法读取未定义的属性(阅读'callHandler')"。请帮我解决这个问题。

  • 我的Flutter代码 *
Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb && kDebugMode && defaultTargetPlatform == TargetPlatform.android) {
    await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  }
  runApp(MaterialApp(home: MyApp()));
}

class MyApp extends StatelessWidget {
  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("InAppWebView test")),
        body: InAppWebView(
          key: webViewKey,
          initialSettings: InAppWebViewSettings(
            javaScriptEnabled: true,
            useShouldOverrideUrlLoading: false,
          ),
          initialUrlRequest: URLRequest(url: WebUri("https://mfkk16.github.io/web_test/")),
          onConsoleMessage: (controller, consoleMessage) => debugPrint(consoleMessage.message),
          onWebViewCreated: (controller) {
            webViewController = controller;
            controller.addJavaScriptHandler(
                handlerName: 'handlerFunction',
                callback: (arguments) {
                  final String data = arguments[0];
                  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
                    content: Text(data),
                    duration: const Duration(seconds: 1),
                  ));
                });
          },
        ));
  }
}
  • 我的网站代码 *
<!DOCTYPE html>
<html lang="en">

<head>
    <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>
    function buttonClick() {
        console.log("button click");
        try {
            window.addEventListener("flutterInAppWebViewPlatformReady", function (event) {
                window.flutter_inappwebview.callHandler('handlerFunction', "got success message");
                console.log("success");
            });
        } catch (error) {
            console.log(error);
        }
    }
</script>

<body>
    <button onclick="buttonClick()" type="button">Show</button>
</body>

</html>
vzgqcmou

vzgqcmou1#

我解决了这个问题

我从Flutter方面解决了这个问题。更新代码在这里…

class MyApp extends StatelessWidget {
  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("InAppWebView test")),
        body: InAppWebView(
          key: webViewKey,
          initialSettings: InAppWebViewSettings(
            javaScriptEnabled: true,
            mediaPlaybackRequiresUserGesture: false,
            cacheEnabled: true,
            allowsInlineMediaPlayback: true,
            useShouldOverrideUrlLoading: true,
          ),
          onConsoleMessage: (controller, consoleMessage) {},
          onWebViewCreated: (controller) async {
            webViewController = controller;
            webViewController!.loadUrl(
              urlRequest: URLRequest(url: WebUri("https://mfkk16.github.io/web_test/")),
            );
          },

          onLoadStart: (controller, url) {
            webViewController!.addJavaScriptHandler(
              handlerName: 'handlerFunction',
              callback: (arguments) {
                toast(arguments, context);
              },
            );
          },
        ));
  }

  void toast(List<dynamic> arguments, BuildContext context) {
    final String data = arguments[0];
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(data), duration: const Duration(seconds: 1)));
  }
}

相关问题