What is the difference between StatefullWidget and StatelessWidget regards to Performance?
Performance-wise, a StatelessWidget
is almost identical to a StatefulWidget
with an empty State
.
Writing:
class Example extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container();
}
}
or:
class Example extends StatefulWidget {
@override
_ExampleState createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
@override
Widget build(BuildContext context) {
return Container();
}
}
won't have any visible impact on the performance of your app.
There indeed is a small gain when using StatelessWidget
here. But it's ridiculously small:
The difference between them can be summarized as calling an empty function vs not calling it. It's something, but absolutely doesn't matter.
The reason being, internally the implementation of StatefulWidget
and StatelessWidget
is almost the same.
StatelessWidget
does have all the extra life-cycles that StatefulWidget
possess.
It has an "initState"/"dispose". Even a setState
!
They are just not part of the public API.