filiph/state_experiments

Question regarding bloc_complex and the ProductSquareBloc

enyo opened this issue · 0 comments

enyo commented

The ProductSquareBloc has its own StreamController for the cartItems. In its constructor it sets up a listener to that stream, to be able to update the isInCard stream:

ProductSquareBloc(Product product) {
  _cartItemsController.stream
    .map((list) => list.any((item) => item.product == product))
    .listen((isInCart) => _isInCartSubject.add(isInCart));
}

then the ProductSquare feeds that _cartItemsController in the _createBloc method:

 _bloc = ProductSquareBloc(widget.product);
_subscription = widget.itemsStream.listen(_bloc.cartItems.add);

My question is this:

Why doesn't the ProductSquareBloc simply get the widget.itemsStream directly?

It could be like this:

/// Creating the bloc
_bloc = ProductSquareBloc(widget.product, onItems: widget.itemsStream);

and inside the bloc's constructor:

ProductSquareBloc(Product product, {Stream onItems}) {
  onItems
    .map((list) => list.any((item) => item.product == product))
    .listen((isInCart) => _isInCartSubject.add(isInCart));
}

What is the advantage of managing it's own controller and stream like this?