skip navigation
skip mega-menu
Flutter Macros Wave Goodbye to Boilerplate | Foresight Mobile

Welcome to the future of Flutter development! 想象一下这样一个世界:编写样板代码的乏味任务已经成为过去. 向…问好 飞镖宏 – your new secret weapon for supercharging productivity. 这些强大的运行时代码生成工具在幕后发挥着它们的魔力, 无缝地减少了样板文件,消除了对繁琐的辅助工具的需求. With Flutter 宏, you can focus on crafting beautiful, high-performance apps while the 宏 h和le the repetitive tasks, 使您的开发过程比以往任何时候都更快、更有效. 准备解锁一个新的水平的编码自由和效率与Flutter宏!

那么,什么 宏?

Dart宏是一个可自定义的代码片段,它接受其他代码作为输入,并对其进行处理 实时 to create, modify, or add new elements. 宏提供了一种可重用的方式来处理重复的任务和模式, 特别是在处理需要迭代类的字段时. Right now t在这里’s one 宏 ready to go: JsonEncodable. As the name suggests, JsonEncodable generates the tedious fromJsontoJson methods for you.

Macros don’t require anything extra to run, 只需向代码中添加所需的适当注释,并在实时生成样板代码时进行观察! With them, code is written to the disk, so say goodbye to the 部分 keyword. Macros directly 增加 the existing class.

What problem 是 they solving

宏,作为 static metaprogramming 工具,提供运行时代码生成解决方案的替代方案(例如 build_runner, json_serializable). Macros eliminate the need for a secondary tool, being integrated into Dart language, 通过Dart工具在后台自动执行您键盘上的每一个按键

  • No extra steps: Macros build your code in 实时 as you write.
  • No performance hit: All the code generation happens directly in the compiler.
  • Clean 和 organised: No extra files cluttering up your project.
  • Clear error messages: 自定义诊断出现在您的IDE中,就像常规编译器消息一样.

例子

Let’s comp是 3 different code snippets, first without any code gen, second with 冻结 和 then third with 宏

No Code Generation

图片标题

对于这样一个通用的样板来说,仅仅是将这部分输入出来所花费的时间就比应该花费的时间长得多. Obviously this is 不 even the whole class, it’s still missing the hashCode, == operator 和 possibly the toString 覆盖, 和 then do this for every (or at least most) model classes, 结果是你花了很多时间在很容易出错的样板代码上.

Now let’s look at 冻结

这看起来比手工编写函数更不容易出错, but now you’ve got 2 new files that you need to work around, push to the project github or set up .gitignore. A lot less work than with manual writing, 但是这些文件中仍然有一些部分可以被视为样板文件.

Now let’s check it with 宏

就是这样! No other code needed to make this work. 在键入最后一个分号时,代码已经生成并准备使用. 写完这个类后,我们可以转到其他文件,并能够做这样的事情:

宏帮助您专注于重要的内容(产品的核心逻辑),并将您从耗时的工作中解放出来, boring tasks that 是 essential but often get in the way.

It’s experimental - how to try it out

Set up the environment:

  1. Switch to the Dart dev channel or flutter master channel
  2. Run 飞镖 –version to make sure you have Dart version `3.5.0-152` or later
  3. 编辑pubspec中的SDK约束以要求Dart版本:SDK: ^3.5.0-152.
  4. Add the package json to dependencies: 飞镖 pub add json.
  5. Enable the experiment in your package's analysis_options.yaml文件. file at the root of your project:

  1. Import the package to the file that you’re planning to use import 'package:json/json.飞镖”;
  1. Run the project with the experimental flag 飞镖 run --enable-experiment=宏 bin/my_app.飞镖

Example of how to create custom 宏

Your first custom 宏 should have two key segments, the 关键字和一个接口,该接口确定为哪个接口确定宏. You can find the list of existing interfaces 在这里.

例如, a 宏 that is applicable to enums, 和 adds new declarations to the enum, would implement the EnumDefinitionMacro interface:

创建了自定义宏后,可以将其添加到声明中,如下所示:

要记住的重要事情是,在高级别的宏使用构建器方法来组合声明的属性 标识符 on those properties, gat在这里d through introspection of the codebase

Stable release timeline

在撰写本文时,还不知道将宏发布到stable中. 具体的日期还没有确定,因为宏是通过深度自省应用它们的代码库来工作的. jsonencoable宏发布的总体目标预计将在2024年底之前发布, 和 the stable release of the full functionality, including creating custom 宏, for early-to-mid 2025.

常见问题解答

What is 增加ation in 飞镖?

通过“增强”关键字的增强是下一步的进化 部分 关键字和部件文件,只是没有额外的语法,使您的类不那么干净. 增强允许在主类文件之外定义类函数.

例如 we have this main class :

Now with the base class set, 我们可以在项目的不同部分创建自己的json序列化方法, 是这样的:

And after adding the import below, you will be good to go!

Why migrate from build_runner/freezed to 宏? 

To improve the developer experience, of course! T在这里 is 不hing wrong with these packages, they do the job perfectly, but with 宏 we make code cleaner by removing the _$ syntax needed for the packages mentioned above, 同时也减少了开发人员管理被忽略的文件的工作量,或者在同行评审中无休止地滚动生成的文件,以获得拉取请求的关键.

Subscribe to our newsletter

在这里注册

" class="hidden">云南工商学院