0

I want to pass some data to flutter from the inappwebview. 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.

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 :

 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);
    });
})     

}

1 Answer 1

0
  • You have not added javascript handler in dart code for 'handlerFooWithArgs' call function, you are listening to handler named 'mySum'. Just check if this is the issue.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.