Reputation: 67
I have a pretty simple question, since we all know in flutter you cant access the initstate method in a stateless widget, but I have been facing some situations where I have a stateless widget that will never need to be rebuilt again but I need to access the initstate method to get data from an API or something else, so my question is, is it worth it to convert said widget to a stateful widget to access the initstate method or should I just call the method I want to call in the build method of a stateless widget.
the reason I'm asking is that I know that a stateful widget is considered expensive for the device.
Upvotes: 2
Views: 225
Reputation: 1633
... I know that a stateful widget is considered expensive for the device.
I dare to differ. Flutter's rebuild is intelligent. Infact Flutter calls the build method of the StatelessWidgets widgets during every repaint.
... is it worth it to convert said widget to a stateful widget to access the initstate method
I feel yes. Don't worry about performance. Flutter is very efficient.
... or should I just call the method I want to call in the build method of a stateless widget.
No please. Flutter is reactive. The build method should be called only when data is ready. The build method is synchronous and should avoid doing any asynchronous computation.
...
Take a deep dive into how Flutter is built at the docs https://docs.flutter.dev/resources/architectural-overview
Upvotes: 1