在Flutter的Freezed中,”equal”属性是否取代了Equatable的需要?

huangapple go评论59阅读模式
英文:

Does the "equal" property in flutter freezed replace the need of Equatable?

问题

以下是您要翻译的内容:

文档:
> 是否生成==/hashcode或不生成
如果为null,则会从项目的build.yaml中获取默认值。
如果该值也为null,仅在类没有自定义==时生成==/hashcode

这是否替代了在比较Freezed创建的类时扩展Equatable的需要?

这些是否会导致相同的行为?

@Freezed(equal: true)
class LanguageState with _$LanguageState {
  const factory LanguageState.initial({required Locale currentLocale}) =
      _Initial;
  const factory LanguageState.languageChanged({required Locale currentLocale}) =
      _LanguageChanged;

以及类似以下内容:

abstract class LanguageState extends Equatable{
  const LanguageState._();
  String get currentLocale; 

  @override
  List<Object?> get props => [currentLocale];
}

class LanguageInitial extends LanguageState{
  const LanguageInitial({required this.currentLocale}) : super._();
  @override
  final String currentLocale;
}

class LanguageChanged extends LanguageState{
  const LanguageChanged({required this.currentLocale}) : super._();
  @override
  final String currentLocale;
}

我正在使用Freezed与bloc,并希望在currentLocale更改时bloc能够更新,即使状态的类型相同。

英文:

The documentation:
> Whether to generate a ==/hashcode or not
If null, picks up the default values from the project's build.yaml.
If that value is null too, generates a ==/hashcode only if the class
does not have a custom ==

Does this replace the need of extending Equitable when comparing classes created by Freezed?

Would these result in the same behaviour?

@Freezed(equal: true)
class LanguageState with _$LanguageState {
  const factory LanguageState.initial({required Locale currentLocale}) =
      _Initial;
  const factory LanguageState.languageChanged({required Locale currentLocale}) =
      _LanguageChanged;

And something like:

abstract class LanguageState extends Equatable{
  const LanguageState._();
  String get currentLocale; 

  @override
  List&lt;Object?&gt; get props =&gt; [currentLocale];
}

class LanguageInitial extends LanguageState{
  const LanguageInitial({required this.currentLocale}) : super._();
  @override
  final String currentLocale;
}

class LanguageChanged extends LanguageState{
  const LanguageChanged({required this.currentLocale}) : super._();
  @override
  final String currentLocale;
}

I am using freezed with bloc and want the bloc to update whenever the currentLocale changes, even if the type of the state is the same.

答案1

得分: 3

当使用fluter_blocfreezed一起时,状态不需要扩展Equatable

为了更简化它一些,除非您明确更改了标准构建配置,否则默认选项将导致相同的结果。即,只需使用注解@freezed

唯一需要的是一种区分状态的方式,如果要在状态发生变化时进行更新。您可以让freezed实现equals和hashcode,让equatable完成,或以任何其他您认为合适的方式完成。

英文:

When using fluter_bloc together with freezed, the states does not have to extend Equatable.

And to simplify it a bit more, the default option will result in the same result unless you've explicitly changed the standard build configuration.. I.e. just using the annotation @freezed

The only thing that you need is a way to distinguish states from each other if you want updates to take place when states have changed. You can let freezed implement equals and hashcode, let equatable do it, or do it yourself in any other way you see fit.

huangapple
  • 本文由 发表于 2023年6月12日 18:30:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定