This should be simple, but I just can't seem to wrap my head around it.
So I have my dartFile.dart and then I have my javaScriptFile.js. How do I link these two so that I can call JavaScript functions declared in my javaScriptFile.js from my dartFile.dart?
I tried to follow the explanation provided in this post, but I guess it was too vague for me :/
Edit: Here's what I tried:
In my javaScriptFile.js, there is nothing but this simple function:
function myFunction() {
return 4;
}
In my dartFile.dart, there is only this blob of code:
import 'package:js/js.dart' as js;
void main() {
print(js.context.myFunction());
}
Following the instructions in the post I mentioned, I added dependencies for js in the pubspec.yaml file. Basically I'm exactly where the asker of the original question was - I can call basic JavaScript functions like alert, but wehn I try to call my function declared in the javaScriptFile.js, I get a 'NoSuchMethodError'
You can just do it like this:
Make sure you include JS before .dart files or make sure all JS files are loaded before running Scripts from Dart
Contents of the dart file:
import 'dart:js';
void main() {
var ret = context.callMethod('myFunction', ['a', 'b']);
var inst = new JsObject(context['someClass'], ['instance']);
print(inst);
}
function myFunction() {
console.log(arguments);
}
function someClass(a) {
this.a = a;
}
This should give you output like
[object Arguments] // from console.log
[object Object] // from print()
Regards, Robert