1

当我在三个月内第一次向 Freezed 类添加一个新属性,然后运行时,我遇到了这个问题

flutter clean && flutter pub get && flutter pub run build_runner build --delete-conflicting-outputs.

我尝试了下一页上的答案,但没有解决问题。

颤振发布运行 build_runner 构建失败

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
4

1 回答 1

0

我有同样的问题。我删除了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
于 2021-11-09T07:28:55.673 回答