Passing object literals

suggest change

It’s common practice in JavaScript to pass object literals to functions:

// JavaScript
printOptions({responsive: true});
Unfortunately we cannot pass Dart Map objects to JavaScript in these cases.

What we have to do is create a Dart object that represents the object literal and contains all of its fields:

// Dart
@JS()
@anonymous
class Options {
  external bool get responsive;

  external factory Options({bool responsive});
}

Note that the Options Dart class doesn’t correspond to any JavaScript class. As such we must mark it with the @anonymous annotation.

Now we can create a stub for the original printOptions function and call it with a new Options object:

// Dart
@JS()
external printOptions(Options options);

printOptions(new Options(responsive: true));

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents