概要
タイトルにある通りです。
FlutterでAPIからデータをとってきたかったので、モデルファイルなど自動生成できる
retrofit / freezedなどのパッケージを使おうとしましたが、retrofit側で依存関係のエラーが発生するので備忘録として記載しておきます。
環境
MacOS Monterey 12.4
Flutter 3.0.2
Dart 12.7.5
バグの発生条件
対象パッケージ
pubspec.yaml
dependencies:
json_annotation:
retrofit:
dio:
freezed: <- 対象
dev_dependencies:
build_runner:
json_serializable:
freezed_annotation:
retrofit_generator: <-対象
発生するエラー
自動生成するコマンドflutter pub run build_runner build
を実行したら以下エラーが発生します。
[INFO] Generating build script...
[INFO] Generating build script completed, took 174ms
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[WARNING] Throwing away cached asset graph because the language version of some package(s) changed. This would most commonly happen when updating dependencies or changing your min sdk constraint.
[INFO] Cleaning up outputs from previous builds....
[INFO] Cleaning up outputs from previous builds. completed, took 6ms
[INFO] Generating build script...
[INFO] Generating build script completed, took 34ms
[WARNING] Invalidated precompiled build script due to missing asset graph.
[INFO] Precompiling build script......
[WARNING] ../../.pub-cache/hosted/pub.dartlang.org/freezed-2.0.4/lib/src/tools/type.dart:27:16: Error: The getter 'imports' isn't defined for the class 'PrefixElement'.
- 'PrefixElement' is from 'package:analyzer/dart/element/element.dart' ('../../.pub-cache/hosted/pub.dartlang.org/analyzer-3.4.1/lib/dart/element/element.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'imports'.
return e.imports.any((l) {
^^^^^^^
[INFO] Precompiling build script... completed, took 557ms
[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
推定される原因
それぞれ登場するパッケージが依存している、analyzerパッケージのバージョンが揃わないことが原因と推測されます。
解決法
pubspec.yaml
に以下を追記
pubspec.yaml
dependencies:
analyzer: 4.2.0
dependency_overrides:
analyzer: ">=4.2.0 <5.0.0"
参考Issue
以上です。
Flutterのバージョンが3になったばかりなので少しこういう不具合は継続するかもしれません。