concept CartBloc in category flutter

appears as: CartBloc, CartBloc
Flutter in Action

This is an excerpt from Manning's book Flutter in Action.

  • In general, blocs and top-level Flutter pages have a one-to-one (1:1) relationship. In reality, the point is that each logical state subject has its own bloc. For example, in the Farmers Market app, I have a CartBloc and a Catalog bloc.
  • 8.3.4. Implementing streams in the CartBloc

    That’s how streams work conceptually, but implementing them is different, of course. As I’m writing this, I’m realizing that blocs are the perfect example to teach streams, because all the stream logic lives side by side in one class. For the rest of this chapter, you’ll implement the CartBloc class and I’ll show the code for streams along the way.

  • Add data to the stream controller from an external source. I set this up in the constructor of the CartBloc class:
    // e_commerce/lib/bloc/cart_bloc.dart -- line 30
    CartBloc(this._service) {
    //...
        _service
            .streamCartCount()
            .listen((int count) =>
                _cartItemCountStreamController.add(count));      1
    !@%STYLE%@!
    {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":6,\"ch\":61},{\"line\":6,\"ch\":62}]]"}
    !@%STYLE%@!
    
    • 1 Anytime you get a new value from the real-time database, add that value to the stream controller.