How do I create a global (on the window) object in Dart lang?
I work on Dart-JS interop. Here is the cleanest way to do it using the new package:js/js.dart interop.
@JS()library your_library;import 'package:js/js.dart';@anonymous@JS()class HelloObject { external factory HelloObject({Function world}); external world();}@JS()external set Hello(HelloObject v);@JS()external HelloObject get Hello;main() { Hello = new HelloObject(world: allowInterop(() { print("Hello from dart"); })); // You can also call this object from Dart as an added bonus. // For example you could call this from Dart or Js. /// Hello.world();}
I am not sure how it will work with objects, but if you want to do that for methods, it's quite simple:
import 'dart:js' as js;main() { js.context['methodFromDart'] = doMyStuff;}void doMyStuff(String text) => print(text);
And then in you Javascript you are free to do:
methodFromDart("Hello world to Dart!");
You can try to find a way how to do similar things to objects.