Appbar widgets not refreshing instantly on calling setState() Flutter

Viewed 124

The AppBar of my app has the cart widget which uses a badge for showing the number of items in the cart. When I place the exact same cart in my body of the Scaffold, it updates the state instantly on adding or removing items from the cart. But in my appbar, the cart widget's state doesn't get updated. It's very weird.

actions: <Widget>[
      Badge(
        badgeContent: Text('${selectedItems.length}',
            style: TextStyle(color: Colors.white)),
        position: BadgePosition.topRight(top: 0.0, right: 5.0),
        badgeColor: Colors.black,
        showBadge: selectedItems.length == 0 ? false : true,
        child: IconButton(
          icon: Icon(Icons.shopping_cart),
          onPressed: () => Navigator.push(context,
              CupertinoPageRoute(builder: (context) => CartScreen())),
          color: Colors.black,
        ),
      )
    ],

As you can see, the selectedItems.length is the number of items in my cart. Please help me out. Stuck here for a long time now...

0 Answers
Related