How to create your first macro in Dart

Thomas Middel
5 min readJun 12, 2024

Done with code generation? Want to know how to speed up your Flutter build times significantly? Macros are here to help you out! A quick beginner’s guide on how to create your own.

Macros are newly introduced in Dart and long-awaited by the community. It adds support for static-meta programming to the Dart language, a major feature.

A Dart macro is a user-definable piece of code that takes in other code as parameters and operates on it in real time to create, modify, or add declarations. This means the code generation with Build Runner that we’re used to can be replaced, making it less error-prone and much faster.

You might not know where to start when writing your first macro. Don’t worry though! We’ll dive into the basics and make sure you have a macro set up at the end of this article 🔥

Be aware that macros are still experimental. We can already work with them, but since they’re not stable yet they’re likely to change. I’ll try to keep this article up-to-date with any future changes.

Setting up your environment

  1. Switch to the Dart dev channel or the Flutter master channel. For people running Homebrew: brew install dart --HEAD
  2. Run dart --version and make sure you have Dart version 3.5.0-152 or later.

--

--