Questions And Answers

More Tutorials

Dart Passing object literals

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
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
external printOptions(Options options);
printOptions(new Options(responsive: true));


In this page (written and validated by ) you learned about Dart Passing object literals . What's Next? If you are interested in completing Dart tutorial, your next topic will be learning about: Dart Date and time.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.