0

Folks, I need some help with this.

I have this code


  void onLoad(int id) async {
    final result = await ListRepository.loadProduct(id);

    if (result != null) {
      product = result;

      WoocommerceModel? model = product!.woo;

      if (model!.woocommerce) {
        List? result2 = await ListRepository.loadOffsiteCoupon(model: model);

        emit(ProductOffsiteCouponSuccess(result2));
        emit(ProductDetailSuccess(product!));

      } else {
        emit(ProductDetailSuccess(product!));
      }
    }
  }

emit(ProductDetailSuccess(product!)); works fine but emit(ProductOffsiteCouponSuccess(result2)); doesn't work.

Here's the state definition:



abstract class ProductDetailState {}

class ProductDetailLoading extends ProductDetailState {}

class ProductOffsiteCouponSuccess extends ProductDetailState {
  final List? coupon;

  ProductOffsiteCouponSuccess(this.coupon);
}

class ProductDetailSuccess extends ProductDetailState {
  final ProductModel product;

  ProductDetailSuccess(this.product);
}

I'm trying to access the data here >>

if (state is ProductOffsiteCouponSuccess) {
            couponList = state.coupon;
           
          }

In this build method >>

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => _productDetailCubit,
      child: BlocBuilder<ProductDetailCubit, ProductDetailState>(
        builder: (context, state) {
          ProductModel? product;
          List? couponList;
          if (state is ProductDetailSuccess) {
            product = state.product;
          }
          if (state is ProductOffsiteCouponSuccess) {
            couponList = state.coupon;
            
          }
          return Scaffold(
            body: _buildContent(product),
            drawer: Drawer(),
          );
        },
      ),
    );
  }
}

Any idea why this doesn't work?

I get the data in the variable 'result2'with no issues, but I can't emit the success state to hold the data for later use.

Any help is appreciated.

Thanks in advance.

fplopez
  • 1
  • 4

1 Answers1

0

If you are using a specific type with coupon (like List) and it is not a String or int then you should extend it and also the abstract class with Equatable and add its variables to props even if this doesn't work then you can try toSet().toList()

Ahsan Ali
  • 1
  • 1
  • 1