filiph / state_experiments

Companion repository to the "Build reactive mobile apps in Flutter" talk
https://www.youtube.com/watch?v=RS36gBEp8OI
910 stars 134 forks source link

Question regarding bloc_complex and the ProductSquareBloc #9

Open enyo opened 6 years ago

enyo commented 6 years ago

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?