If you just started learning flutter and heard about state or state management. And you are confused about state. So this post is for you. And your all doubt will be cleared after reading this post.
Let’s first discuss about difference between Stateful and Stateless widget in flutter. As there name suggests.
Stateful widget is used if you have changable UI elements (like Text, Image etc) which can be changed anytime. Then we consider using Stateful widgets.
Suppose if you have Text()
widget on screen and you want to change its text on button click, then you will simply change state of text and it will automatically reflect on Text()
widget and you will see new text displayed on screen.
class MyScreen extends StatefulWidget {_MyScreenState createState() => _MyScreenState();}class _MyScreenState extends State<MyScreen> {String _name = 'Website Name';Widget build(BuildContext context) {return Scaffold(body: Column(children: [Text(_name),RaisedButton(child: Text('Display Name'),onPress: () {setState(() {_name = 'Learn Pain Less';});})]),);}}
As in above example on button press we have changed _name
variable to ‘Learn Pain Less’ and its displayed on screen. But if we try to change name directly (without setState) then it will not display on screen. So that’s the main use of Stateful widget in Flutter. Whenever state is changed the build()
function will recall and all UI elements are rebuild. So sometime this becomes the point of performance issue. Because if we have 40 widgets in our build()
function and we update state of 1 widget then rest of 39 widgets get rebuild unnecessarily.
If you want to display some static UI elements then you may consider Stateless widget. Because you are not going to change their value in future. Thats why Stateless widgets doesn’t affect performace of our app. Because build()
function of Stateless widget is called once.
Let say if you want to display your name on screen, and you don’t want to change it.
class MyApp extends StatelessWidget {String _name = 'Learn Pain Less';Widget build(BuildContext context) {return Scaffold(child: Text(_name),);}}
As you just read if you change state of any widget in Stateful widget then the whole widget hierarchy got rebuild, which is not good thing. So if we consider using state management library then state will be managed somewhere else outside our widget. And because now state is not going to manage inside our widget then we can use Stateless widget, because we use Stateless widget when we don’t want to handle state of any widget.
Also read about Why use RxDart and how to use with BLoC Pattern in Flutter? which is also linked with State management in Flutter.
LESSONS
COURSES
TUTORS
Quick Links
Legal Stuff
Social Media