Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Use Set State

This video was last updated on Sep 22 2022

setState() is the primary method used to update set, but you’ll quickly learn that while setState will update the widget tree, it may do it in away that you don’t quite expect.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

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),
          ),
        )

Reviews

Comments