i've been trying to update state from bloc for hours and didnt work, i even try to check the properties of the state before yield and all properties is updated but the state wont.
here is my code
Bloc
@override
Stream<HomeState> mapEventToState(
HomeEvent event,
) async* {
if (event is HomeStarted) {
yield HomeLoadSuccess(carts: []);
}
if (event is HomeCartAdded) {
var currentState = state as HomeLoadSuccess;
var carts = addCart(currentState.carts, event.item, event.qty);
print(carts);
//output carts working as expected but the state doesnt change/update
//yield HomeInitial(); uncomment this line works but meh
yield currentState.copyWith(carts: carts);
}
}
Event
abstract class HomeEvent {}
class HomeStarted extends HomeEvent {}
class HomeCartAdded extends HomeEvent {
final Item item;
final double qty;
HomeCartAdded({required this.item, this.qty = 0.0});
}
State
abstract class HomeState extends Equatable {
@override
List<Object> get props => [];
}
class HomeInitial extends HomeState {}
class HomeLoadInProgress extends HomeState {}
class HomeLoadSuccess extends HomeState {
final List<Cart> carts;
HomeLoadSuccess({
required this.carts,
});
HomeLoadSuccess copyWith({List<Cart>? carts}) {
return HomeLoadSuccess(
carts: carts ?? this.carts,
);
}
@override
List<Object> get props => [carts];
}
Item Class
class Item extends Equatable {
int id;
String name;
String picture;
double price;
Item({
required this.id,
required this.name,
required this.picture,
required this.price,
});
@override
List<Object> get props => [id, name, picture, price];
}
Cart Class
//class item
class Cart extends Equatable {
Item item;
double qty;
double total;
Cart({
required this.item,
required this.qty,
required this.total,
});
@override
List<Object> get props => [item, qty, total];
}
is there anything wrong with my code or am i missing something here ? im able to get this code working with prev version of bloc.. thanks