Suragch
1 min readSep 13, 2021

--

This is a question from a reader who messaged me on Twitter. I'm answering here so that more people can benefit from the Q&A (and perhaps point out a better approach):

"Hey there! I've recently read your Flutter article about minimalist state management and began to implement it. I've one question: for example if I make a request to a web server, and a business error occurs. Either my service or my state manager handle the error and want to indicate the UI to show a snackbar. How can it be done? Do I really need to create a ValueListenableBuilder somewhere in order to react to it?"

When you make a request to a web server, your app can be in one of serveral states: (1) the initial state before you make the request, (2) waiting for a reply from the server, (3) a successful responce from the server, or (4) an error response from the server.

I would make an enum for these four states and then create a ValueNotifier on the state management side for that enum. Then on the UI side, use a ValueListenableBuilder to show a different UI layout for each possible state. When you build the error state you can also add a method that will trigger a snackbar to show. (Something like this.)

--

--

Suragch
Suragch

Written by Suragch

Flutter and Dart developer. Twitter: @suragch1, Email: suragch@suragch.dev

Responses (2)