setState on Flutter works only the first time

Asked

Viewed 45 times

0

I’m studying Flutter and decided to make a cart, I have the product widget and widget to list products.

The problem is that when I first click on add it adds in the cart and updates the amount in the widget list, (from 0 goes to 1 in real time), but if I click again it adds in the cart but does not update the widget.

If I change screen and then go back to the home screen, widgets are updated

What could be the problem?


Product Widget

                       Expanded(
                        flex: 2,
                        child: IconButton(
                            icon: Icon(Icons.remove),
                            onPressed: () async{
                              await widget.cart.remove(widget.produto);
                              setState(() {
                                
                              });
                            }),
                      ),
                      SizedBox(
                        width: 10,
                        child: TextField(
                          keyboardType: TextInputType.number,
                          decoration: InputDecoration(
                              hintText: '${widget.produto.quantidade}',
                              border: InputBorder.none),
                        ),
                      ),
                      Expanded(
                        flex: 2,
                        child: IconButton(
                            icon: Icon(Icons.add),
                            onPressed: () async{
                              await widget.cart.add(widget.produto);
                              setState(() {
                                
                              });
                            }),
                      ),            

Widget to list products:

class ListProducts extends StatefulWidget {
  List<Produto> _products;
  ListProducts(this._products);

  @override
  _ListProductsState createState() => _ListProductsState();
}

class _ListProductsState extends State<ListProducts> {
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget._products.length,
      itemBuilder: (BuildContext ctx, int index) {
        return ProdutoWidget(widget._products[index]);
      },
    );
  }
}
  • You don’t have the entire code in the question, but from what I understand, you have two Statefulwidget, right? The list, and the list element, right? And you want to, from inside the inside widget, give setState in the most external? If that’s it, the setState’s you showed won’t work. The simplest way is to add a callback to the internal widget that calls the setstate in the external widget. Also, there is some motivation for inner widget to be stateful?

  • 1

    Got it, so inside the product widget I would pass a callback pro widget listing the new state? Really the product widget doesn’t need to be stateful

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.