Managing State in Flutter

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

Part 2: Use Provider

13. Understand Consumers

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: 12. Use Context Methods Next episode: 14. Use Multiple Providers

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've reached locked video content where the transcript will be shown as obfuscated text.

So far, we’ve been getting a reference to our data object and then simply used it in our builder method. If this is all you are doing, you can opt to use a Consumer instead. A consumer gets your data from the Provider and unwraps it for you to use in a builder method.

Widget build(BuildContext context) {
    return Consumer<Pillar>(
        builder: (_, pillar, __) => Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
                const Center(
                child: TutorialWidget(),
                ),
                Padding(
                padding: const EdgeInsets.only(top: 24.0),
                child: Text(
                    'Total Tutorials: ${pillar.articleCount}',
                    style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
                ),
                )
            ],
        ),
    );
}
return Consumer<Pillar>(builder: (_, pillar, __) {
    return Stack(
    children: [
        InkWell(
        onTap: () {
            pillar.increaseArticleCount();
        },
        child: Image.asset('assets/images/${pillar.type.imageName}',
            width: 110, height: 110),
        ),
        Positioned(
        bottom: 2,
        child: CircleAvatar(
            backgroundColor: Colors.blue,
            child: Text(pillar.articleCount.toString()),
        ),
        )
    ],
    );
});