![](/img/trans.png)
[英]How to fix Flutter build_runner error: Failed to precompile build_runner:build_runner:
[英]flutter build_runner takes too long
我正在使用EasyLocalization
、 mobx
、 JsonSerializable
等。要生成*.g.dart
文件,我使用build_runner watch
或scripts/build.sh
中的腳本:
flutter packages pub run build_runner build
最多需要2 秒,但突然間開始需要至少10 秒,有時甚至是30 秒。 由於這種情況, build_runner watch
變得無用,我的意思是從那以后它就不能正常工作了。
運行腳本時控制台上的 output :
$ 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_localization
和mobx
? 我不知道,我是新手。
除了避免在不必要的輸入上運行構建器之外,我還設法通過禁用我沒有使用的 mockito 模擬生成器來加快構建速度:
targets:
$default:
builders:
your_builder:
generate_for:
- lib/**/*.dart
mockito:mockBuilder:
enabled: false
運行構建導致 Mockito 在我的每個測試文件中搜索@GenerateMocks。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.