Why do we need to use the Equatable class with flutter_bloc? Also, what do we use the props for? Below is sample code for making a state using the bloc pattern in Flutter.
abstract class LoginStates extends Equatable{}
class LoginInitialState extends LoginStates{
@override
List<Object> get props => [];
}
For comparison of data, we required
Equatable. it overrides==andhashCodeinternally, which saves a lot of boilerplate code. InBloc, we have to extendEquatabletoStates and Eventsclasses to use this functionality.So, that means
LoginStateswill not make duplicate calls and will not going to rebuild the widget if the same state occurs.Define State:
Define State with props:
propsdeclared when we wantStateto be compared against the values which declared inside props ListIf we remove the username from the list and keep a list like
[this.status], thenStatewill only consider thestatusfield, avoiding theusernamefield. That is why we used props for handling State changes.Bloc Stream Usage:
As we extending
State with Equatablethat makes a comparison of old state data with new state data. As an example let's look at the below example here LoginData will build a widget only once, which will avoid the second call as it is duplicated.