0

我创建了一个实现冻结的自定义类,我正在尝试将保存的设置对象与当前设置对象进行比较,当我将两个对象与相同的值进行比较时,它返回的结果不一样

问题是因为dateFormat, 因为其他值有效

Settings settings1 = Settings(
    dateFormat: DateFormat('dd/MM/yyyy'),
    movementType: StandardMovement.type905,
    autoBatch: true,
);
Settings settings2 = Settings(
    dateFormat: DateFormat('dd/MM/yyyy'),
    movementType: StandardMovement.type905,
    autoBatch: true,
);
print(settings1 == settings2);

实际上我需要更多的对象,但我只是dateFormat为了更容易阅读而放置

import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:intl/intl.dart';
part 'settings.freezed.dart';
part 'settings.g.dart';

enum StandardMovement { type905, type101 }

@freezed
class Settings with _$Settings {
  const factory Settings({
    /// Example:
    /// ```dart
    /// DateFormat('dd.MM.yyyy');
    /// ```
    @JsonKey(
      fromJson: _dateFormatFromJson,
      toJson: _dateFormatToJson,
      name: 'dateFormat',
    )
    required DateFormat dateFormat,

    required StandardMovement movementType,

    required bool autoBatch,
  }) = _Settings;

  factory Settings.fromJson(Map<String, dynamic> json) =>
      _$SettingsFromJson(json);
}

DateFormat _dateFormatFromJson(String pattern) => DateFormat(pattern);
String _dateFormatToJson(DateFormat dateFormat) => dateFormat.pattern!;

编辑:解决

你可以看看@下面的评论,但 tldr 和 Equatable:


@freezed
class Settings extends Equatable with _$Settings {
  const Settings._();

  @override
  List<Object?> get props => [
        dateFormat.pattern,
        movementType,
        autoBatch,
      ];
... etc
4

2 回答 2

1

让它与 Equatable 一起工作,例如:


@freezed // Extended Equatable
class Settings extends Equatable with _$Settings {
  const Settings._(); // <- Added this cause if not I got errors adding equatable

  @override
  List<Object?> get props => [ // <- custom class properties to check
        dateFormat.pattern,
        movementType,
        autoBatch,
      ];

我也得到了这个没有 Equatable 的工作:

enum StandardMovement { type905, type101 }

@freezed
class Settings with _$Settings {

  const Settings._(); // <- Added

  const factory Settings({
    /// Example:
    /// ```dart
    /// DateFormat('dd.MM.yyyy');
    /// ```
    @JsonKey(
      fromJson: _dateFormatFromJson,
      toJson: _dateFormatToJson,
      name: 'dateFormat',
    )
    required DateFormat dateFormat,

    required StandardMovement movementType,

    required bool autoBatch,
  }) = _Settings;

  factory Settings.fromJson(Map<String, dynamic> json) =>
      _$SettingsFromJson(json);

  @override // <- Added
  bool operator ==(dynamic other) {
    return identical(this, other) ||
        (
          other.runtimeType == runtimeType && other is _Settings &&
          const DeepCollectionEquality().equals(other.dateFormat.pattern, dateFormat.pattern) && // <- Custom check
          const DeepCollectionEquality().equals(other.movementType, movementType) &&
          const DeepCollectionEquality().equals(other.autoBatch, autoBatch)
        );
  }

  @override // <- Added
  int get hashCode => Object.hash(
        runtimeType,
        const DeepCollectionEquality().hash(dateFormat.locale), // <- Custom check
        const DeepCollectionEquality().hash(movementType),
        const DeepCollectionEquality().hash(autoBatch),
      );
}

DateFormat _dateFormatFromJson(String pattern) => DateFormat(pattern);
String _dateFormatToJson(DateFormat dateFormat) => dateFormat.pattern!;
于 2022-02-03T15:33:33.343 回答
0

没用过freezed,但你能覆盖 的相等运算符Settings吗?如果是这样,您可以添加以下内容:

enum StandardMovement { type905, type101 }

// SAMPLE GENERATED CLASS
@immutable
abstract class _$Settings {
  const _$Settings(this.dateFormat, this.standardMovement);

  final DateFormat dateFormat;
  final StandardMovement standardMovement;
}

class Settings extends _$Settings {
  const Settings(DateFormat dateFormat, StandardMovement standardMovement) : super(dateFormat, standardMovement);

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) {
      return true;
    }

    return other is _$Settings 
      && other.dateFormat == dateFormat // <- Update this by adding your custom equality checks
      && other.standardMovement == standardMovement;
  }

  @override
  int get hashCode => dateFormat.hashCode ^ standardMovement.hashCode;
}

如果要在检查中包含一些自定义类属性,则需要覆盖相等运算符。

于 2022-02-03T13:00:48.020 回答