Guide on testing new Dart / Flutter feature: Macros (JsonCodable)
In this article, I’m testing the preview version of macros in Dart language.

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)

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

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
json
package to your 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);
}

- Call the
fromJson
andtoJson
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

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.