Flutter 无法使用 Freezed 构建 build_runner
Posted
技术标签:
【中文标题】Flutter 无法使用 Freezed 构建 build_runner【英文标题】:Flutter failed to build build_runner with Freezed 【发布时间】:2021-12-04 17:00:50 【问题描述】:当我三个月来第一次向 Freezed 类添加新属性,然后运行时,我遇到了这个问题
flutter clean && flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs
.
我尝试了下一页的答案,但没有解决问题。
flutter pub run build_runner build failed
flutter clean
flutter pub cache repair
flutter pub get
flutter pub run build_runner clean
flutter pub run build_runner build --delete-conflicting-outputs
以下是我修改前后的代码
//Before
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:~/models/item/item.dart';
part 'user.freezed.dart';
part 'user.g.dart';
part 'user_controller.dart';
@freezed
abstract class User with _$User
const factory User(
@Default('') String name,
@Default(35.6620) double latitude,
@Default(139.7038) double longitude,
) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
//After
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:~/models/item/item.dart';
part 'user.freezed.dart';
part 'user.g.dart';
part 'user_controller.dart';
@freezed
abstract class User with _$User
const factory User(
@Default('') String name,
@Default(35.6620) double latitude,
@Default(139.7038) double longitude,
int age,
@Default('') String birthday,
@Default('') String gender,
@Default('') String imageURL,
@Default('') String introduction,
@Default('') String restaurant,
@DateTimeConverter() DateTime updateAt,
@DateTimeConverter() DateTime createAt,
@Default('') String uri,
) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
class DateTimeConverter implements JsonConverter<DateTime, Timestamp>
const DateTimeConverter();
@override
DateTime fromJson(Timestamp value)
return DateTime.parse(value.toDate().toString());
@override
Timestamp toJson(DateTime value) => Timestamp.fromDate(value);
以下是错误提示
Running "flutter pub get" in my_code... 2,962ms
Failed to build build_runner:build_runner:
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:258:50: Error: The property 'displayString' is defined in multiple extensions for 'TargetKind' and neither is more specific.
- 'TargetKind' is from 'package:meta/meta_meta.dart' ('../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart').
Try using an explicit extension application of the wanted extension or hiding unwanted extensions from scope.
var kindNames = kinds.map((kind) => kind.displayString).toList()
^^^^^^^^^^^^^
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:1950:14: Context: This is one of the extension members.
String get displayString
^^^^^^^^^^^^^
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/meta-1.7.0/lib/meta_meta.dart:91:14: Context: This is one of the extension members.
String get displayString
^^^^^^^^^^^^^
../../Developer/flutter/.pub-cache/hosted/pub.dartlang.org/analyzer-0.41.2/lib/src/error/best_practices_verifier.dart:260:36: Error: The getter 'commaSeparatedWithOr' isn't defined for the class 'List<dynamic>'.
- 'List' is from 'dart:core'.
Try correcting the name to the name of an existing getter, or defining a getter or field named 'commaSeparatedWithOr'.
var validKinds = kindNames.commaSeparatedWithOr;
^^^^^^^^^^^^^^^^^^^^
pub finished with exit code 1
【问题讨论】:
同样的问题:/ 【参考方案1】:我遇到了同样的问题。我删除了flutter SDK,并重新安装了相同的版本。它开始起作用了。
我没有使用 freezed tho,但我的一个依赖项正在使用它。
如果有帮助,这些是我的 dev_dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
json_serializable: ^3.5.0
retrofit_generator: ^1.4.1+3
hive_generator: ^1.0.0
build_runner: ^1.11.5
【讨论】:
感谢您的帮助。 “删除了flutter SDK,并重新安装了相同的版本”这对我有用!!!以上是关于Flutter 无法使用 Freezed 构建 build_runner的主要内容,如果未能解决你的问题,请参考以下文章
Flutter:读取 build.yaml 文件中传递给 build_runner 的变量
为啥我在为 Flutter 使用 codemagic 构建代码时出现“无法为 iOS 构建”?