I was wondering how to use a javascript library in dart ? There are several examples in the web but either they do not work, or they are super verbose, and they are all at least 2 years old.
I want to use the D3 library in my case
Thanks
The likely simplest method of using javascript in dart is to use immediately is the dart:js library, where you can use context.callMethod to call methods in javascript.
Ex.
import 'dart:js';
main() => context.callMethod('alert', ['Hello from Dart!']);//'alert' is the function name, the second parameter is a `List` of arguments passed to the function.
Alternatively, you can use package:js, which should usually be used and provides higher level functionality, but it may be harder to start using because the documentation is a bit spotty. This library uses annotations with the external keyword to handle dart-JS interop.
Ex.
@JS()
library stringify;
import 'package:js/js.dart';
// Calls invoke JavaScript `JSON.stringify(obj)`.
@JS('JSON.stringify')
external String stringify(Object obj);
With this, you can then call stringify elsewhere and static analysis will be able to better pick up on type errors because you can explicitly define parameter and return types.
stringify ?library <>; wouldn't matter still, but what's in the @JS annotation would.