Managing State in Flutter

Sep 22 2022 · Dart 2.17, Flutter 3.0, Android Studio Chipmunk

Part 1: Understand State Management

04. Use Set State

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 03. Meet the Sample App Next episode: 05. Add a Value Notifier

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

One of the first tools in your state management toolkit is setState. This is a method defined the state class. When you call setState, you pass in a void callback which is called right away. This callback should only contain changes that affect any changes to current state. Everything else should be placed outside of the set state callback.

import 'models/pillar.dart';
final pillarData = Pillar(type: PillarType.flutter, articleCount: 115);
import '../models/pillar.dart';
  final Pillar pillar;
  const TutorialWidget({required this.pillar, super.key});
import '../models/pillar.dart';
final Pillar pillar;
const TutorialsPage({required this.pillar, super.key});
children: <Widget>[
  Center(child: TutorialWidget(pillar: widget.pillar)),
  const Padding(
body: TutorialsPage(pillar: pillarData),
setState(() {
  widget.pillar.increaseArticleCount();
});
Image.asset('assets/images/${widget.pillar.type.imageName}', width: 110, height: 110),
Positioned(
          bottom: 2,
          child: CircleAvatar(
            backgroundColor: Colors.blue,
            child: Text(widget.pillar.articleCount.toString()),
          ),
        )
Padding(
          padding: const EdgeInsets.only(top: 24.0),
          child: Text(
            'Total Tutorials: ${widget.pillar.articleCount}',
            style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
          ),
        )