Re: How can setState() be called in a Stateless Widget?

7,075 views
Skip to first unread message
Message has been deleted

Steven McDowall

unread,
Mar 4, 2019, 6:21:04 AM3/4/19
to Kamil Geagea, Flutter Dev
I don't see where setState() is being called in a Stateless widget at all ?  I see setState being used in _ProductManagerState which is certainly a STATE widget .. 

class _ProductManagerState extends State<ProductManager> {


Maybe read this? 





On Mar 3, 2019, at 11:09 PM, Kamil Geagea <KamilGe...@gmail.com> wrote:

Hello, I am following this online course where we just separated the RaisedButton from the Stateful Widget to a Stateless Widget created in the State object.
I don't understand how setState() is called in a StatelessWidget...
When we create the element ProductControl we pass the function _addProduct() that calls setState(). And when the button is pressed, setState() is called in ProductControl which is a Stateless Widget...
Can someone explain me how does it work please? Thank you

Here is some code:
//StatefulWidget
class ProductManager extends StatefulWidget {
final String startingProduct;

ProductManager({this.startingProduct = 'Sweets Tester'});

@override
State<StatefulWidget> createState() {
return _ProductManagerState();
}
}

class _ProductManagerState extends State<ProductManager> {
List<String> _products = [];

@override
void initState() {

super.initState();
_products.add(widget.startingProduct);
}

void _addProduct(String product) {
setState((){
_products.add(product);
});
}

@override
Widget build(BuildContext context) {
return Column(
children: <Widget> [
Container(
margin: EdgeInsets.all(10.0),
child: ProductControl(_addProduct),
),
Products(_products),
]
);
}
}

// ProductControl() in another file

class ProductControl extends StatelessWidget {
final Function addProduct;

ProductControl(this.addProduct);

@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
addProduct('Sweets');
},
child: Text('Add Product'),
);
}
}

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pedro Massango

unread,
Mar 4, 2019, 6:35:58 AM3/4/19
to Steven McDowall, Kamil Geagea, Flutter Dev
setState() can't be called in a statelessWidget

Ian B

unread,
Mar 4, 2019, 6:37:34 AM3/4/19
to Kamil Geagea, Flutter Dev
I think the issue you are probably seeing, is that the call is instigated from a stateless widget, but that function was passed in via a stateful widget.

So when setState is called, it's actually referencing the _ProductManagerState.

It may be helpful to look at topics like closures in methods, as it's related.


Ian Hickson

unread,
Mar 6, 2019, 5:31:00 PM3/6/19
to Kamil Geagea, Flutter Dev
The difference between a StatefulWidget and a StatelessWidget is that a StatefulWidget has state and a StatelessWidget does not. You can setState when you have state, and when you do not have state, you cannot setState.
--
Ian Hickson

Mykhailo Yarushevskyi

unread,
Mar 8, 2019, 4:31:50 PM3/8/19
to Flutter Dev
In your case, your ProductManager (StateFulWidget) via constructor of ProductControl (StatelessWidget) shares access to an instance of a class Function '_addProduct', and you call this method from the instance of the ProductManager when clicked a RaisedButton 'Add Product'. Since this method executing in the ProductManager (StateFulWidget) context, therefore management of the state (a renewval ProductManager as StateFulWidget) via setState() method work well.


понеділок, 4 березня 2019 р. 06:09:25 UTC+2 користувач Kamil Geagea написав:
Reply all
Reply to author
Forward
0 new messages