I am building flutter app where i need to use some javascript code it works correctly if i call it on window.onload but when i call it in dart code it does not work at all on button press in ElevatedButton. although debug ,console is being logged but not the javafunctions from dart code as it is unable to find those functions.
I have checked my javascript files are being loaded correctly but still the dart side of code is not able to find the functions declared in there although those functions are being called correctly in start or window.onload:
Now i am convinced the issue is on dart/flutter side but unable to find it: below are functions and error
i have verified if my custom.js file is being loaded correctly, functions names are correct and there is no typos.
But issue is when i click the button and javascript code does not work at all
const init = () =>{
console.log("Hello World");
}
window.onload = ()=>{
init();
sendMessageToUnity('yup');
}
const sendMessageToUnity = (message) =>{
console.log("Message being sent to Unity: " + message);
var iframe = document.getElementById('unityIframe');
iframe.contentWindow.postMessage(message, '*');
}
init is being call
Hello World
custom.js:15 Message being sent to Unity: yup
void callJavaScriptFunction() {
js.context.callMethod('sendMessageToUnity', ['I am Flutter']);
js.context.callMethod("init");
}
ElevatedButton(
onPressed: () {
// Define your function here
print("I am Pressed");
callJavaScriptFunction();
},
i am pressed being printed but does not proceed further
he following NoSuchMethodError was thrown while handling a gesture:
'sendMessageToUnity'
method not found
Receiver: Instance of 'Window'
Arguments: ["I am Flutter"]
When the exception was thrown, this was the stack:
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 296:3 throw_
dart-sdk/lib/_internal/js_dev_runtime/patch/js_patch.dart 143:7 callMethod
packages/limuweb/iframescreen.dart 66:6 callJavaScriptFunction
packages/limuweb/iframescreen.dart 52:15 <fn>
packages/flutter/src/material/ink_well.dart 1170:21 handleTap
packages/flutter/src/gestures/recognizer.dart 351:24 invokeCallback
packages/flutter/src/gestures/tap.dart 656:11 handleTapUp
packages/flutter/src/gestures/tap.dart 313:5 [_checkUp]
packages/flutter/src/gestures/tap.dart 246:7 handlePrimaryPointer
packages/flutter/src/gestures/recognizer.dart 703:9 handleEvent
packages/flutter/src/gestures/pointer_router.dart 98:7 [_dispatch]
packages/flutter/src/gestures/pointer_router.dart 143:9 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/linked_hash_map.dart 21:7 forEach
packages/flutter/src/gestures/pointer_router.dart 141:17 [_dispatchEventToRoutes]
packages/flutter/src/gestures/pointer_router.dart 127:7 route
packages/flutter/src/gestures/binding.dart 501:5 handleEvent
packages/flutter/src/gestures/binding.dart 481:14 dispatchEvent
packages/flutter/src/rendering/binding.dart 450:11 dispatchEvent
packages/flutter/src/gestures/binding.dart 426:7 [_handlePointerEventImmediately]
packages/flutter/src/gestures/binding.dart 389:5 handlePointerEvent
packages/flutter/src/gestures/binding.dart 336:7 [_flushPointerEventQueue]
packages/flutter/src/gestures/binding.dart 305:9 [_handlePointerDataPacket]
lib/_engine/engine/platform_dispatcher.dart 1423:5 invoke1
lib/_engine/engine/platform_dispatcher.dart 336:5 invokeOnPointerDataPacket
lib/_engine/engine/pointer_binding.dart 405:30 [_sendToFramework]
lib/_engine/engine/pointer_binding.dart 225:7 onPointerData
lib/_engine/engine/pointer_binding.dart 1035:20 <fn>
lib/_engine/engine/pointer_binding.dart 948:7 <fn>
lib/_engine/engine/pointer_binding.dart 541:9 loggedHandler
dart-sdk/lib/_internal/js_dev_runtime/patch/js_allow_interop_patch.dart 188:27 _callDartFunctionFast1
Handler: "onTap"
Recognizer: TapGestureRecognizer#1321c
debugOwner: GestureDetector
state: possible
won arena
finalPosition: Offset(709.0, 919.0)
finalLocalPosition: Offset(709.0, 6.0)
button: 1
sent tap down