I want to implement flutter bloc in my app. I want to compare the length of the list and trigger blocListener accordingly. But when using listenWhen, the parameter which it provides(previousState and currentState) both show the current length of the list. I expected that these two params will be giving different states through which I could compare the length of the list on different states.
BlocListener<ListBloc, ListCubitState>(
listener: (context, state){
print("new order");
},
listenWhen: (previous, current){
if(current.items.length > previous.items.length){
return true;
}
print(**current.items.length.toString()**);
print(**previous.items.length.toString()**);
return false;
},
My cubit class:
class ListBloc extends Cubit<ListCubitState>{
ListBloc(): super(ListCubitState(items: ["items", "items"]));
addItem() => emit(ListCubitState(items: addItemToList(state.items)));
removeItem() => emit(ListCubitState(items: removeItemToList(state.items)));
List<String> addItemToList(List<String> item){
List<String> newList = item;
newList.add("adsfasf");
return newList;
}
List<String> removeItemToList(List<String> item){
List<String> newList = item;
newList.removeLast();
return newList;
}
}
Could anyone suggest what's wrong in the code (or) Is there any other way to implement it?