Problem with events in BLoC Flutter. I always call two events instead of one
This is the culprit:
abstract class TodoState extends Equatable {
const TodoState();
@override
List<Object> get props => [];
}
You are extending Equatable
in TodoState
and passing an empty list to props
. When other states such as TodoLoadedState
extend TodoState
they inherit Equatable
as well and the empty props.
If you're using Equatable make sure to pass all properties to the props getter.
This is from bloc faq. Right now all instances of your TodoLoadedState
are considered equal. Doesn't matter if you have a TodoLoadedState
with hundreds of loadedUser
or a TodoLoadedState
with none. They are both considered equal and only the first time you pass a new TodoLoadedState
the BlocBuilder
will update. The consequent ones have no effect since BlocBuilder
thinks it is the same as previous one. The reason your LoadTodos
event causes a rebuild is that first you emit TodoLoadingState()
and then in case of success TodoLoadedState(loadedUser: _loadedTodoList)
. This alternating between two different states makes it work.
So either don't use Equatable
or make sure to pass all the properties to props
.
class TodoLoadedState extends TodoState {
final List<dynamic> loadedUser;
TodoLoadedState({required this.loadedUser});
@override
List<Object?> get props => [loadedUser];
}