ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect…

Follow publication

Guide on testing new Dart / Flutter feature: Macros (JsonCodable)

In this article, I’m testing the preview version of macros in Dart language.

“cool digital art dart light blue looking at the sunrise.”

Macros

The Dart version 3.5.0-152 introduces a preview of the first macro, the JsonCodable macro. The macro feature is like code generation, except it is instant and handled by the Dart language. JsonCodable is available from the Dart dev channel or the Flutter master channel. In the future, users will be able to create their own macros. Macros are very powerful tool for code generation tasks, such as JSON serialization, before being done by external tools like JsonSerializable.

JsonCodable Macro

Many of you have created applications that need to call an outside service to fetch data. Often, this data is returned in a format like JSON. To interact with or show the data to users, you should map it to Dart objects. This process from JSON to object is serialization; the inverse is deserialization. As mentioned above, this has traditionally been done using something like JsonSerializable or other applications; however, now we can use the JsonCodable macro.

Setup

flutter channel master <- Change Flutter build channel

flutter channel <- Check that you are on the right channel (master)

Flutter channel

dart --version <- Check that you are on the right version (3.5.0-152 or above)

Flutter version

flutter create json_codable_test <- Create a test project

  • Open your test app and add macros as an experimental feature under analyzer to analysis_options.yaml.
analyzer:
enable-experiment:
- macros
Add macros to analysis_options.yaml.
  • Add json package to your pubspec.yaml.
json package to pubspec.yaml.
  • Create a simple example class, such as Person.
import 'package:json/json.dart';

@JsonCodable()
class Person {
String name;
int age;

Person(this.name, this.age);
}
Person class with JsonCodable annotation.
  • Call the fromJson and toJson functions from, for example, main.
main() {
Person person = Person.fromJson({
"age": 30,
"name": "John Doe",
"email": "",
});

print(person.toJson());
}

flutter run --enable-experiment=macros <- Run the Flutter test app with macros enabled.

Example

Example of using JsonCodable with Visual Studio Code. Pretty cool!

Conclusion

From playing around a little with this feature, it sure does make it easier and less stressful to generate code. With third-party tools, you always have to worry about whether they are in sync and if their versions are right. But here, I can trust Dart to handle the code generation. Obviously, this feature is in a preview state, and changes may occur, so I wouldn’t use it in any critical application, but it does make me feel hopeful for the future of Dart macros.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

No responses yet

Write a response