I'm new to using Bloc and Cubit so I'm trying to figure out some best practices specifically with the State component. I have a simple Todos app where the Todos can be in multiple different states:
part of 'todos_cubit.dart';
abstract class TodosState extends Equatable {
const TodosState();
@override
List<Object> get props => [];
}
class TodosLoading extends TodosState {}
class TodosLoaded extends TodosState {
final List<Todo> todos;
TodosLoaded(this.todos);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is TodosLoaded && listEquals(other.todos, todos);
}
@override
int get hashCode => todos.hashCode;
}
class TodosEmpty extends TodosState {}
class TodosError extends TodosState {
final String error;
TodosError(this.error);
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is TodosError && other.error == error;
}
@override
int get hashCode => error.hashCode;
}
My question is, should I keep the List<Todo> todos
in the TodosLoaded
subclass or should it be moved to the base class? My thoughts are that by moving it to the base class, it would make my TodosEmpty
state redundant because I could simple check to see if the list of todos is empty in the UI. If this is the case, should I also move the String error
to the base class?
Im sure there are pros and cons to each approach, just hoping to bounce ideas off anyone with more experience with Bloc.