Flutter bloc new version deprecated mapEventToState
Asked Answered
U

1

6

I have a bloc hierarchy, where in the child bloc's mapEvenToState I used super.mapEventToState. In the newer version of the bloc package, mapEventToState is deprecated.

What should I use instead of super.mapEventToState? I know about on<Event>, but what is the equivalent of super.mapEventToState?

Ungenerous answered 4/4, 2022 at 18:17 Comment(0)
E
11

it should be something like that in your bloc class

class ProductsBloc extends Bloc<ProductsEvent, ProductsState> {
  final GetMoreProducts moreProductsUsecase;
  final GetProducts getProductsUsecase;

  ProductsBloc({
    required this.moreProductsUsecase,
    required this.getProductsUsecase,
  }) : super(ProductsInitial()) {

    on<GetProductsEvent>(_onGetProducts);

  }

and the function call can be like this

_onGetProducts(GetProductsEvent event, Emitter<ProductsState> emit) async {
    emit(LoadingProductsState());
    var result = await getProductsUsecase();
    result.fold(
        (l) => emit(LoadFailedState()),
        (r) => { emit(ProductsLoadedState(products: products, isReachedMax: false)),
            });
  }
Enesco answered 5/4, 2022 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.