11

I have a script given me by third party API that I should perform on the client side. For that I need to somehow call this JS function from dart. I've seen that there's a JS Library out in the pub, but I don't know exactly what I should do. Can somebody explain please?

1
  • 1
    show to us Code Commented Nov 5, 2019 at 0:54

1 Answer 1

10

Here is a very good tutorial on how to use javascript libraries in Dart:

https://dev.to/graphicbeacon/how-to-use-javascript-libraries-in-your-dart-applications--4mc6

It uses the js package and gives a very easy solution.

In my case, I needed to call a js method from a Flutter app embedded on a webpage. The js method was on the main html page on which the Flutter app was embedded.

In this case, you need to use the parent selector to target the js method: @JS('parent.jsFunction')

@JS()
library main;

import 'package:js/js.dart';

@JS('parent.jsFunction')
external void jsFunction(dynamic command, dynamic arg);

void callJsFunction() {
  jsFunction('command', 'args');
}
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.