SharedAppData class
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.
To create a local project with this code sample, run:
flutter create --sample=widgets.SharedAppData.1 mysample
To create a local project with this code sample, run:
flutter create --sample=widgets.SharedAppData.2 mysample
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- SharedAppData
Constructors
-
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.
const
Properties
- child → Widget
-
The widget below this widget in the tree.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< StatefulWidget> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
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 ofkey
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
forkey
and rebuilds any widgets that have created a dependency onkey
with SharedAppData.getValue.