0

I have this code in cubit, where I try to get initial value of cart item count and then to subscribe for stream changes.

class HomeCubit extends Cubit<HomeState> {
  late GetCollectionsUseCase _collectionsUseCase;
  late GetCartItemsCountUseCase _cartItemsCountUseCase;
  StreamSubscription<int>? _cartItemCountSubscription;
  List<Collection> _cachedCollections = [];

  HomeCubit() : super(HomeInitial()) {
    _collectionsUseCase = GetCollectionsUseCase();
    _cartItemsCountUseCase = GetCartItemsCountUseCase();
    getInitialCartItemCount();
  }

  void _listenForCartItemCountChanges() {
    print("ZORAN _listenForCartItemCountChanges");
    _cartItemCountSubscription = _cartItemsCountUseCase.execute(NoParams()).listen(
          (itemCount) {
            print("ZORAN listen of _listenForCartItemCountChanges");
            emit(CartItemCountUpdated(itemCount));
      },
      onError: (error) {
        print("ZORAN onError of _listenForCartItemCountChanges");
        emit(CartItemCountUpdated(0));
      },
    );
  }

  Future<void> getInitialCartItemCount() async {
    try {
      print("ZORAN getInitialCartItemCount");
      _listenForCartItemCountChanges();
      int initialItemCount = await _cartItemsCountUseCase.executeInitial(NoParams());
      emit(CartItemCountUpdated(initialItemCount));
    } catch (e) {
      print("ZORAN onError of getInitialCartItemCount");
      emit(CartItemCountUpdated(0));
    }
  }

  @override
  Future<void> close() {
    print("ZORAN close of HomeCubit");
    _cartItemsCountUseCase.close();
    _cartItemCountSubscription?.cancel();
    return super.close();
  }

This is my use case:

class GetCartItemsCountUseCase extends StreamUseCase<int, NoParams> {
  late CartRepository _cartRepository;

  GetCartItemsCountUseCase() {
    _cartRepository = ShopifyCartRepository();
  }

  @override
  Stream<int> execute(NoParams params) async* {
    print("ZORAN execute of GetCartItemsCountUseCase");
    try {
      yield* _cartRepository.stream;
    } catch (e) {
      print("ZORAN execute: "+e.toString());
      rethrow;
    }
  }

  Future<int> executeInitial(NoParams params) async {
    print("ZORAN executeInitial of GetCartItemsCountUseCase");
    try {
      String? cartId = await _cartRepository.getCartId();
      if (cartId == null) {
        throw CartEmptyException();
      }

      return _cartRepository.getInitialCartItemsCount(cartId);
    } catch (e) {
      print("ZORAN executeInitial: "+e.toString());
      rethrow;
    }
  }

  void close() {
    print("ZORAN close of GetCartItemsCountUseCase");
    _cartRepository.closeStream();
  }
}

This is my CartRepository where I add items to StreamController and from where I try to get instance of stream to use case and then back to cubit.

class CartRepository implements CartRepository {

  late ApiService _apiService;

  final StreamController<int> streamController = StreamController<int>();

  @override
  Stream<int> get stream => streamController.stream;

  CartRepository() {
    _apiService = ApiService();
  }

  // Emit the new count value to the main stream
  void _notifyCartItemCountChanged(int newCount) {
    print("ZORAN _notifyCartItemCountChanged: "+newCount.toString());
    streamController.sink.add(newCount);
  }

  @override
  void closeStream() {
    print("ZORAN closeStream");
    streamController.close();
  }

  @override
  Future<int> getInitialCartItemsCount(String cartId) async {
    print("ZORAN getInitialCartItemsCount");
    const FlutterSecureStorage secureStorage = FlutterSecureStorage();
    final itemCountKey = '$cartId';

    // Retrieve the initial count value from secure storage
    String? itemCountValue = await secureStorage.read(key: itemCountKey);
    int initialItemCount = int.parse(itemCountValue ?? '0');
    _notifyCartItemCountChanged(initialItemCount);
    return initialItemCount;
  }

Any ideas?

Zookey
  • 2,637
  • 13
  • 46
  • 80

0 Answers0