0% found this document useful (0 votes)
22 views2 pages

Setstate & Initstate

Uploaded by

ismailovich1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views2 pages

Setstate & Initstate

Uploaded by

ismailovich1904
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

setState() & initState

In Flutter, `setState` and `initState` are commonly used methods in stateful widgets, playing crucial roles in
managing state and initializing the widget, respectively. Here's a breakdown of each:

initState:
initState: is a lifecycle method that is called when the stateful widget is inserted into the widget tree.
This method is used to perform any initialization that needs to occur before the widget is built.
- It is called once when the stateful widget is created.
- It is often used to initialize data or set up listeners.
- You must call `super.initState()` to ensure that the parent class's initialization code runs.

Example:
class MyStatefulWidget extends StatefulWidget {
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {


@override
void initState() {
super.initState();
// Initialization code here
}

@override
Widget build(BuildContext context) {
return Container(
// Widget build code here
);
}
}

setState:
`setState` is a method that you call whenever you want to update the state of the widget and reflect those
changes in the UI. It tells the framework that the state has changed and the widget needs to be rebuilt.

Key Points about `setState`:


- It triggers a rebuild of the widget by calling the `build` method.
- You only need to update the state inside the `setState` method.
- The framework optimizes performance by only rebuilding the parts of the widget tree that need
updating.

Example:

class MyStatefulWidget extends StatefulWidget {


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

class _MyStatefulWidgetState extends State<MyStatefulWidget> {


int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Counter App'),
),
body: Center(
child: Text(
'Counter: $_counter',
style: TextStyle(fontSize: 24),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
child: Icon(Icons.add),
),
);
}
}

How initState and setState Work Together


- `initState` is called once when the widget is first inserted into the widget tree. This is where you can
initialize any state or start any animations, fetch initial data, or set up listeners.
- `setState` is called whenever you need to update the state of the widget. Inside the `setState` method,
you can modify the state variables and the framework will call the `build` method to update the UI.

Flutter Page 1
you can modify the state variables and the framework will call the `build` method to update the UI.

Flutter Page 2

You might also like