I am quite new to this library and BLOC pattern too, I need fetch all products and a single product based on a particular id.
I have seen many examples created using this library , those people are getting parameter by event.paramter
In my case its not working.
Q. How to get event properties while extending equatable?? inside bloc.
events.dart
abstract class ProductsEvent extends Equatable {
const ProductsEvent();
}
class FetchProducts extends Equatable{
@override
List<Object> get props => [];
}
class FetchSingleProduct extends Equatable{
final int id;
const FetchSingleProduct({@required this.id}) : assert(id !=null);
@override
List<Object> get props => [id];
}
Bloc.dart
class ProductsBloc extends Bloc<ProductsEvent, ProductsState> {
final AbstractProductsRepository abstractProductsRepository;
ProductsBloc(this.abstractProductsRepository);
@override
ProductsState get initialState => InitialProductsState();
@override
Stream<ProductsState> mapEventToState(
ProductsEvent event,
) async* {
yield ProductsLoadingState();
if(event is FetchProducts){
try{
final productsData = await abstractProductsRepository.fetchProducts();
yield ProductsLoadedState(productsData);
}on Error{
print("Error in Block");
yield ProductErrorState();
}
}else if(event is FetchSingleProduct){
try{
//Event.id - not working.
final productData = await abstractProductsRepository.fetchSingleProduct(**event.id**);
yield SingleProductLoaded(productData);
}on Error{
print("Error in Block");
yield ProductErrorState();
}
}
}
}