Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Talking with JavaScript


If we take into account the enormous amount of JavaScript code and libraries that exist, and are still being developed, it is very important that we have a simple way to use JavaScript code from within Dart applications, in particular to get access from Dart to the JavaScript code that is running in the same web page. The earliest attempts used window.postMessage, and then a package called js was built. Because of the huge importance of this topic, the Dart team now has provided us with a core library, dart:js, to interoperate with JavaScript. This provides better performance, reduces the size of the compiled JavaScript file, and makes it also easier to use. Once dart:js is ready, the package js has been rewritten to use dart:js under the covers.

How to do it...

Take a look at the project js_interop, as explained in the following steps:

  1. To start using dart:js in our project, we have to import it in our code:

    import 'dart:js';
  2. In js_interop.html, we declare a Dart script...