DEV Community

Cover image for Use Effects for One-Time UI Work
YeLinAung for dust

Posted on Originally published at dust-codegen.pages.dev

Use Effects for One-Time UI Work

Flutter snackbar callback code beside a Dust State effect listener example

Flutter shows snackbars through ScaffoldMessenger.

That belongs at the UI edge.

The tricky part is deciding where the event comes from.

If a save action lives in a ViewModel, the widget should not need to own the whole save flow just to show “Saved”.

Dust State has effects for this.

Emit a small event from the ViewModel. Listen with the generated listener widget. Show the snackbar there.

Effects do not change state. They are one-time UI events for mounted listeners.

So the child widget does not rebuild because a snackbar was shown.

Use state for facts that must survive rebuilds. Use effects for short UI work like snackbars or navigation.

Read the Dust State effects guide.

Dust #StateManagement #Flutter #Dart #CodeGeneration

Top comments (0)