SharedAppData class Null safety

Enables sharing key/value data with its child and all of the child's descendants.

  • SharedAppData.getValue(context, key, initCallback) creates a dependency on the key and returns the value for the key from the shared data table. If no value exists for key then the initCallback is used to create the initial value.

  • SharedAppData.setValue(context, key, value) changes the value of an entry in the shared data table and forces widgets that depend on that entry to be rebuilt.

A widget whose build method uses SharedAppData.getValue(context, keyword, initCallback) creates a dependency on the SharedAppData. When the value of keyword changes with SharedAppData.setValue(), the widget will be rebuilt. The values managed by the SharedAppData are expected to be immutable: intrinsic changes to values will not cause dependent widgets to be rebuilt.

An instance of this widget is created automatically by WidgetsApp.

There are many ways to share data with a widget subtree. This class is based on InheritedModel, which is an InheritedWidget. It's intended to be used by packages that need to share a modest number of values among their own components.

SharedAppData is not intended to be a substitute for Provider or any of the other general purpose application state systems. SharedAppData is for situations where a package's custom widgets need to share one or a handful of immutable data objects that can be lazily initialized. It exists so that packages like that can deliver custom widgets without requiring the developer to add a package-specific umbrella widget to their application.

A good way to create an SharedAppData key that avoids potential collisions with other packages is to use a static Object() value. The SharedObject example below does this.

The following sample demonstrates using the automatically created SharedAppData. Button presses cause changes to the values for keys 'foo', and 'bar', and those changes only cause the widgets that depend on those keys to be rebuilt.
To create a local project with this code sample, run:
flutter create --sample=widgets.SharedAppData.1 mysample

The following sample demonstrates how a single lazily computed value could be shared within an app. A Flutter package that provided custom widgets might use this approach to share a (possibly private) value with instances of those widgets.
To create a local project with this code sample, run:
flutter create --sample=widgets.SharedAppData.2 mysample



SharedAppData({Key? key, required Widget child})
Creates a widget based on InheritedModel that supports build dependencies qualified by keywords. Descendant widgets create such dependencies with SharedAppData.getValue and they trigger rebuilds with SharedAppData.setValue. [...]


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() State<StatefulWidget>
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

getValue<K extends Object, V>(BuildContext context, K key, SharedAppDataInitCallback<V> init) → V
Returns the app model's value for key and ensures that each time the value of key is changed with SharedAppData.setValue, the specified context will be rebuilt. [...]
setValue<K extends Object, V>(BuildContext context, K key, V value) → void
Changes the app model's value for key and rebuilds any widgets that have created a dependency on key with SharedAppData.getValue. [...]