简体   繁体   English

如何将数据从 inappwebview 传递到点击按钮时颤动?

[英]How to pass data from inappwebview to flutter on button click?

I want to pass some data to flutter from the inappwebview.我想将一些数据从 inappwebview 传递给 flutter。 I tied using the below code, But it's not working.我使用下面的代码绑定,但它不起作用。 I need to send some data as a json to my dart function for the api call.我需要将一些数据作为 json 发送到我的 dart 函数以进行 api 调用。

child: InAppWebView(
                        initialFile:
                            "assets/blockly_webview/pages/index.html",
                        initialOptions: options,
                        onWebViewCreated: (controller) {
                          controller.addJavaScriptHandler(
                              handlerName: 'handlerFoo',
                              callback: (args) {
                                // return data to the JavaScript side!
                                return {
                                  'env_type': DynamicUI.envType,
                                  'parent_blocks':
                                      currentLevelStatus.parentBlock,
                                  'child_blocks':
                                      currentLevelStatus.childBlock,
                                  'userAccessToken': userAccessToken,
                                  'device_list': userDeviceController
                                      .deviceList[0].deviceId
                                };
                              });
                          controller.addJavaScriptHandler(
                              handlerName: "mySum",
                              callback: (args) {
                                // Here you receive all the arguments from the JavaScript side
                                // that is a List<dynamic>
                                print("From the JavaScript side:");
                                print(args);
                                return args
                                    .reduce((curr, next) => curr + next);
                              });
                        },
                      ),

Js code: js代码:

 function send_Data(){
console.log("call Function is called");
window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
    window.flutter_inappwebview.callHandler('handlerFoo')
      .then(function(result) {
        // print to the console the data coming
        // from the Flutter side.
        console.log(JSON.stringify(result));
        
        window.flutter_inappwebview
          .callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}, result);
    });
})     

} }

  • You have not added javascript handler in dart code for 'handlerFooWithArgs' call function, you are listening to handler named 'mySum'.您尚未在 dart 代码中为“handlerFooWithArgs”调用函数添加 javascript 处理程序,您正在侦听名为“mySum”的处理程序。 Just check if this is the issue.只需检查这是否是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM