Managing state in Flutter is similar to managing state in React, but with some key differences. I’ll explain the concepts with trivial examples and compare them to equivalent situations in React.
1. StatefulWidget vs. StatelessWidget
In Flutter, you can create two types of widgets: StatefulWidget and StatelessWidget. StatefulWidget is used when you need to manage state, while StatelessWidget is used when you don’t need to manage state.
Flutter Example:
// StatefulWidget
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
],
);
}
}
React Equivalent:
import React, { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
In both examples, we have a counter that increments when a button is pressed. In Flutter, we use a StatefulWidget and manage the state using the _CounterState class. In React, we use the useState hook to manage the state.
2. setState() vs. useState()
In Flutter, you use setState() to update the state of a StatefulWidget. In React, you use the useState() hook to update the state.
Flutter Example:
class _CounterState extends State<Counter> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
}
React Equivalent:
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
In both examples, we update the state by calling a function that increments the counter.
3. Stateful vs. Stateless Widgets
In Flutter, you can create stateful or stateless widgets. Stateful widgets manage their own state, while stateless widgets rely on their parent widget to manage their state.
Flutter Example:
// Stateful widget
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $_count'),
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
],
);
}
}
// Stateless widget
class DisplayCount extends StatelessWidget {
final int count;
DisplayCount({required this.count});
@override
Widget build(BuildContext context) {
return Text('Count: $count');
}
}
React Equivalent:
// Stateful component
function Counter() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>Increment</button>
</div>
);
}
// Stateless component
function DisplayCount({ count }) {
return <p>Count: {count}</p>;
}
In both examples, we have a stateful widget/component that manages its own state and a stateless widget/component that relies on its parent to manage its state.
4. Lifting State Up
In Flutter, you can lift state up to a parent widget by passing the state as a parameter to the child widget.
Flutter Example:
class ParentWidget extends StatefulWidget {
@override
_ParentWidgetState createState() => _ParentWidgetState();
}
class _ParentWidgetState extends State<ParentWidget> {
int _count = 0;
void _increment() {
setState(() {
_count++;
});
}
@override
Widget build(BuildContext context) {
return Column(
children: [
DisplayCount(count: _count),
ElevatedButton(
onPressed: _increment,
child: Text('Increment'),
),
],
);
}
}
class DisplayCount extends StatelessWidget {
final int count;
DisplayCount({required this.count});
@override
Widget build(BuildContext context) {
return Text('Count: $count');
}
}
React Equivalent:
function ParentComponent() {
const [count, setCount] = useState(0);
const increment = () => {
setCount(count + 1);
};
return (
<div>
<DisplayCount count={count} />
<button onClick={increment}>Increment</button>
</div>
);
}
function DisplayCount({ count }) {
return <p>Count: {count}</p>;
}
In both examples, we lift the state up to the parent widget/component and pass it as a parameter to the child widget/component.
That’s a brief overview of managing state in Flutter and React. While there are some differences in syntax and architecture, the concepts are similar.

Leave a Reply