Dung (Donny) Nguyen

Senior Software Engineer

Enable macros for Dart and Flutter Projects

To enable macros for Dart and Flutter projects, you need to follow a few key steps:

Update Flutter and Dart

First, ensure you’re using the latest Flutter version that supports macros:

  1. Switch to the master channel:
    flutter channel master
    flutter upgrade
    
  2. Update your pubspec.yaml to use a DEV version of Dart:
    environment:
      sdk: ^3.7.0-243.0.dev
    

Enable Macros in Project Configuration

  1. Create or update your analysis_options.yaml file in the project root:
    analyzer:
      enable-experiment:
        - macros
    

Configure VS Code for Macros

To enable macros at runtime in VS Code:

  1. Create or open the .vscode/launch.json file in your project.

  2. Add a new configuration or modify an existing one to include the macros experiment flag:

    a. For Dart CLI project:

     ```json
     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "Dart CLI",
                 "request": "launch",
                 "type": "dart",
                 "vmAdditionalArgs": ["--enable-experiment=macros"]
             }
         ]
     }
     ```
    

    b. For Flutter project:

     {
         "version": "0.2.0",
         "configurations": [
             {
                 "name": "Flutter",
                 "request": "launch",
                 "type": "dart",
                 "toolArgs": [
                     "--enable-experiment=macros"
                 ]
             }
         ]
     }
    

This configuration ensures that when you run or debug your Dart CLI or Flutter application from VS Code, it will enable the macros experiment.

Additional Steps

By following these steps, you’ll have macros enabled in your Dart CLI or Flutter project, both for analysis and at runtime when launching from VS Code.