1

I am making a list of stateless widget as shown below and passing the id as the parameter to the widgets.

Code for cartPage:-

class Cart extends StatefulWidget {
  @override
  _CartState createState() => _CartState();
}

class _CartState extends State<Cart> {
  bool loading=true;
  List<CartTile> cartTiles=[];

  @override
  void initState() {
    super.initState();
    if(currentUser!=null)
      getData();
  }

  getData()async
  {
    QuerySnapshot snapshot=await cartReference.doc(currentUser.id).collection('cartItems').limit(5).get();
    snapshot.docs.forEach((doc) {
      cartTiles.add(CartTile(id: doc.data()['id'],index: cartTiles.length,));
    });
    setState(() {
      loading=false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: loading?Center(child:CircularProgressIndicator():SingleChildScrollView(
                child: Column(
                  children: cartTiles,
                ),
              ),
    );
  }
}

Code for CartTile:-

    class CartTile extends StatelessWidget {
      final String id;
      CartTile({this.id,});
    
      @override
      Widget build(BuildContext context) {
        return StreamBuilder(
          stream: productReference.doc(id).snapshots(),
          builder: (context,snapshot)
          {
//here am using the snapshot to build the cartTile.
          },
        );
      }
    }

So, my question is whenever I will call setState in my homepage then will the stateless widget be rebuilt and increase my document reads. Because i read somewhere that when we pass the same arguments or parameters to a stateless widget then due to its cache mechanism it doesn't re build. If it will increase my reads then is there any other way to solve this problem?

Deepak Lohmod
  • 2,072
  • 2
  • 8
  • 18

0 Answers0