ScaffoldMessenger class Null safety

Manages SnackBars for descendant Scaffolds.

This class provides APIs for showing snack bars.

To display a snack bar, obtain the ScaffoldMessengerState for the current BuildContext via ScaffoldMessenger.of and use the ScaffoldMessengerState.showSnackBar function.

When the ScaffoldMessenger has nested Scaffold descendants, the ScaffoldMessenger will only present a SnackBar to the root Scaffold of the subtree of Scaffolds. In order to show SnackBars in the inner, nested Scaffolds, set a new scope for your SnackBars by instantiating a new ScaffoldMessenger in between the levels of nesting.

Here is an example of showing a SnackBar when the user presses a button.
To create a local project with this code sample, run:
flutter create --sample=material.ScaffoldMessenger.1 mysample

See also:



ScaffoldMessenger({Key? key, required Widget child})
Creates a widget that manages SnackBars for Scaffold descendants.


child Widget
The widget below this widget in the tree. [...]
hashCode int
The hash code for this object. [...]
@nonVirtual, read-only, inherited
key Key?
Controls how one widget replaces another widget in the tree. [...]
final, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree. [...]
createState() ScaffoldMessengerState
Creates the mutable state for this widget at a given location in the tree. [...]
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children. [...]
@protected, inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node. [...]
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep. [...]
toString({DiagnosticLevel minLevel =}) String
A string representation of this object. [...]
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants. [...]
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object. [...]
toStringShort() String
A short, textual description of this widget.


operator ==(Object other) bool
The equality operator. [...]
@nonVirtual, inherited

Static Methods

maybeOf(BuildContext context) ScaffoldMessengerState?
The state from the closest instance of this class that encloses the given context, if any. [...]
of(BuildContext context) ScaffoldMessengerState
The state from the closest instance of this class that encloses the given context. [...]