Search code examples
flutterblocequatablefreezedflutter-freezed

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


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<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;
}

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.


Solution

  • 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.