SharedAppDataModifier 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 in˚itial 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.
{@tool dartpad}
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.
** See code in examples/api/lib/widgets/shared_app_data/shared_app_data.0.dart ** {@end-tool}
{@tool dartpad} 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.
** See code in examples/api/lib/widgets/shared_app_data/shared_app_data.1.dart ** {@end-tool}
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatelessWidget
- SingleChildStatelessModifier
- SharedAppDataModifier
- Available extensions
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
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- modifierKey → Key?
-
The actual key of the widget, which Modifier wrapped
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
build(
BuildContext context) → Widget -
Describes the part of the user interface represented by this widget.
inherited
-
buildWithChild(
BuildContext context, Widget? child) → Widget -
A build method that receives an extra
child
parameter.override -
createElement(
) → SingleChildStatelessElement -
Create a SingleChildStatelessElement
inherited
-
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