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