当我在三个月内第一次向 Freezed 类添加一个新属性,然后运行时,我遇到了这个问题
flutter clean && flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs
.
我尝试了下一页上的答案,但没有解决问题。
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