calling javascript from Dart
First add the js package as dependency in your pubspec.yaml
:
dependencies:
js: any
Then you can use your own js function myFunc()
like that :
import 'package:js/js.dart' as js;
main() {
js.context.myFunc();
}
js.context
is an alias to javascript window
.
See Using JavaScript from Dart: The js Library for more details.
Maybe my answer will be worth it for somebody, so that's why I'm posting a simple JS function call from Dart.
- Add the
js
package dependency:dependencies: js: any
- Create a JS file, let's say
example.js
:function test() { return 12+20; }
- Add the
example.js
above insideindex.html
with the<script src="...">
tag. - Interop the function above from JS to Dart:
@JS() library t; import 'package:js/js.dart'; @JS() external int Test(); class MyOwn { int get value => Test(); }
- And, in AngularDart's TODOLIST — which is default component available —:
@override Future<Null> ngOnInit() async => print(MyOwn().value);