How to show a notification Toast with Riverpod

Flutter tips Published on

This Flutter tip demonstrates a method to show toast notifications globally using Riverpod, effectively eliminating the common requirement of passing BuildContext around.
By utilizing a GlobalKey attached to your app's navigator, you gain access to a root BuildContext.
A Riverpod Provider then encapsulates the toast display logic.
(using a package like another_flushbar in the example), which leverages this globally accessible context.
This allows developers to easily trigger toasts from anywhere in the application, such as directly from Riverpod notifiers after an asynchronous operation, without needing to manually manage or pass a widget's BuildContext.

Frequently Asked Questions

Why is needing BuildContext for toasts sometimes a problem?

Many UI operations, like showing dialogs or toasts, require a BuildContext to understand where in the widget tree they should be displayed. When you want to trigger such an action from outside the widget tree (e.g., from a Riverpod notifier, a service class, or after a background task), you might not have a BuildContext readily available.

How does using a GlobalKey help with this?

By attaching a GlobalKey to your app's navigator, you can access the root BuildContext from anywhere in your app. This allows you to show toasts or dialogs without needing to pass around BuildContext.

Create a 5 stars app using our Flutter templates

Check our flutter boilerplate
kickstarter for flutter apps
Read more
You may also be interested in
Published on
Published on
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved