skip to Main Content

Can we use the equatable on navigation, form validation?…..also explain me on which cases we can use equatble in which we can’t use it

i am a beginner in BLoC so i need to know the cases in which we can use eqautable package in BLoC

2

Answers


  1. Your question is not very specific. So in general, you would use equatable if you compare to data class objects. Or in other words, if you intend to compare instances of a class you would declare them as Equatable.
    Example: You have a class rectangle and you want to check if rectangle A has the same size as rectangle B => A == B?

    If on the other hand you only compare variables within the class, then you don’t need Equatable
    Example: you only have comparisons of the kind A.length == B.length

    With regards to Bloc, if I am not mistaken you would use Equatable if you update the same state. The opposite makes this easier to understand: Without Equatable you need to change the state (and not only the data within the state) in order to update your screen. If you want to update your same state eg MyFormUpdate with new data, then you would need to yield MyFormInProgress in between. Yielding 2 MyFormUpdate states directly after one another, would only show the data of the first state

    Login or Signup to reply.
  2. Equatable is highly recommended to use if you are emitting a new state by copying the earlier state. By default the new state is not emitted if its same as the earlier state. Using Equatable is an easier way to deal with such conditions.

    Consider the following case of LoginFormState with username and password. Everytime the username or password is changed, you can emit a new state using the copyWith() method.

    class LoginFormState extends Equatable {
    
      LoginFormState({this.username, this.password});
    
      final String? username;
      final String? password;
    
      LoginFormState copyWith({String? username, String? password}) {
        return LoginFormState(
          username: username ?? this.username,
          password: password ?? this.password,
        );
      }
    
      @override
      List<Object?> get props = [username, password];
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search