'Flutter stream doesn't listen or detect change

I have problem, i have 3 dart files,

home.dart contain button with onclick:

final cartEmiter = CartEmitter();
cartEmiter.emitCart("add_cart");

cart.dart contain:

class CartEmitter {
  StreamController _controller = StreamController.broadcast();
  void emitCart(action) {
    _controller.add(action);
    // print(action);
  }

  Stream get cartAction => _controller.stream;
}

and in main.dart I have this code to change the cart badge.

StreamSubscription _cartCountSubscribtion;

int _cartCount = 0;

@override
  void initState() {
    _cartCountSubscribtion = CartEmitter().cartAction.listen((action) {
      print(action);
      setState(() {
         _cartCount++;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    _cartCountSubscribtion.cancel();
    super.dispose();
  }

But it doesn't work, no error, no output printed. Is my code wrong or how to listen to change?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source