我创建了一个实现冻结的自定义类,我正在尝试将保存的设置对象与当前设置对象进行比较,当我将两个对象与相同的值进行比较时,它返回的结果不一样
问题是因为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