Managing State in Flutter

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

Part 1: Understand State Management

06. Extend a Value Notifier

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: 05. Add a Value Notifier Next episode: 07. Meet the Inherited Widget

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

When we last left, we defined a value notifier to let us know when to update our state. The value notifier was so closely related to our model object, a better solution would be to just have our model extend the value notifier. Let’s do this now.

class Pillar extends ValueNotifier<int> {
Pillar({required this.type, int articleCount = 10}) : super(articleCount);
class Pillar extends ValueNotifier<int> {
  int get articleCount => value;
  var active = true;
  final PillarType type;
void increaseArticleCount({int by = 1}) {
    value += by;
}
@override
void initState() {
    pillarData.addListener(() {
        setState(() {});
    }); 
    super.initState();
}
@override
void dispose() {
    pillarData.dispose();
    super.dispose();
}
body: TutorialsPage(pillar: pillarData),
const TutorialsPage({required this.pillar, super.key});
child: TutorialWidget(pillar: widget.pillar),
const TutorialWidget({required this.pillar, super.key});
widget.pillar.increaseArticleCount(by: 1);
ValueListenableBuilder(
    valueListenable: pillarData,
    builder: (context, value, child) {
        return TutorialsPage(pillar: pillarData);
    }),