flutter build_runner 耗时太长

flutter build_runner takes too long

我正在使用 EasyLocalizationmobxJsonSerializable 等。要生成 *.g.dart 文件,我正在使用 build_runner watch 或 [= 中的脚本18=]:

flutter packages pub run build_runner build

最多需要 2 秒,但突然开始至少需要 10 秒 或有时 30秒。由于这种情况,build_runner watch 变得没用了,我的意思是从那以后它就不能正常工作了。

当我 运行 我的脚本时控制台上的输出:

$ sh scripts/build.sh 
[INFO] Generating build script...
[INFO] Generating build script completed, took 611ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 126ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 745ms

[INFO] Running build...
[INFO] 1.2s elapsed, 0/16 actions completed.
[INFO] 2.2s elapsed, 0/16 actions completed.
[INFO] 3.3s elapsed, 0/16 actions completed.
[INFO] 12.5s elapsed, 1/17 actions completed.
[INFO] 13.6s elapsed, 4/20 actions completed.
[INFO] Running build completed, took 14.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 57ms

[INFO] Succeeded after 14.6s with 2 outputs (47 actions)

我最终决定尝试自己解决这个问题,我通过在我的 pubspec.yaml 中添加以下内容来稍微改进它:例如

targets:
  $default:
    builders:
      json_serializable:
        enabled: true
        generate_for:
          include:
            - lib/your_codegen_folder/**.dart
            - lib/**.gen.dart

希望这能为您加快速度。您必须为其他两个生成器添加构建器配置,例如此处的 json_serializable 生成器。我怀疑他们可能被称为 easy_localizationmobx?我不知道,我是新手。