JsonCodable (Dart Macros experiment)

Did you know?

As of Dart 3.5.0 (master channel), you can preview the experimental JsonCodable macro.

This macro augments your classes with fromJson and toJson methods, and it works in realtime as you edit your code (no code generation is needed).

Example:

// Example showing how to use the JsonCodable macro import 'package:json/json.dart'; @JsonCodable() class Movie { final int id; final String title; } void main() { const movieJson = { 'id': 5, 'title': 'Dune', }; final movie = Movie.fromJson(movieJson); print(movie.toJson()); }

Note that as of May 2024, JsonCodable is not configurable and has two big limitations:

  • field names must exactly match the keys in the maps
  • default values are not supported

If you want to have a play with it, read the official docs for how to enable it:

Happy coding!

Want More?

Invest in yourself with my high-quality Flutter courses.

Flutter Foundations Course

Flutter Foundations Course

Learn about State Management, App Architecture, Navigation, Testing, and much more by building a Flutter eCommerce app on iOS, Android, and web.

Flutter & Firebase Masterclass

Flutter & Firebase Masterclass

Learn about Firebase Auth, Cloud Firestore, Cloud Functions, Stripe payments, and much more by building a full-stack eCommerce app with Flutter & Firebase.

The Complete Dart Developer Guide

The Complete Dart Developer Guide

Learn Dart Programming in depth. Includes: basic to advanced topics, exercises, and projects. Fully updated to Dart 2.15.

Flutter Animations Masterclass

Flutter Animations Masterclass

Master Flutter animations and build a completely custom habit tracking application.