Passing object literals
suggest changeIt’s common practice in JavaScript to pass object literals to functions:
// JavaScript
printOptions({responsive: true});
Unfortunately, a Dart Map cannot be passed as a JavaScript object literal.
What we have to do is create a Dart object that represents the object literal and contains all of its fields:
// Dart
import 'package:js/js.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 void printOptions(Options options);
void main() {
printOptions(new Options(responsive: true));
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents