WithValue<T> class
A Flutter widget that inherits from InheritedWidget to provide a specific value down the widget tree.
This widget is generic and can be used with any type of value. It is particularly useful for providing data directly to multiple widgets in the subtree without having to pass the data through multiple widget constructors.
Generics: T - The type of the value being provided down the widget tree.
Parameters:
value - The data value to be provided to the widgets in the subtree.
IMPORTANT: To ensure proper functionality, if the value is not a primitive type, it should override the ==
and hashCode
methods.
child - The widget below this widget in the tree. This widget can access the provided value.
key - An optional key that controls how one widget replaces another widget in the tree.
WithValue includes WithValue.of to retrieve the nearest WithValue up the widget tree
and return its value. If the InheritedWidget
is not found, a AssertionError
is thrown.
Example Usage:
WithValue<int>(
value: 42,
child: MyWidget(),
)
To retrieve the value:
// Access the value in a descendant widget
int value = WithValue.of<int>(context);
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- ProxyWidget
- InheritedWidget
- WithValue
Constructors
- WithValue.new({required Widget child, required T value, Key? key})
-
const
Properties
- child → Widget
-
The widget below this widget in the tree.
finalinherited
- 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
- value → T
-
The value provided to the subtree.
final
Methods
-
createElement(
) → InheritedElement -
Inflates this configuration to a concrete instance.
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, int wrapWidth = 65}) → 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
-
updateShouldNotify(
covariant WithValue< T> oldWidget) → bool -
IMPORTANT: To ensure proper functionality if
value
is not a primitive type it should override the==
andhashCode
methods.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
maybeOf<
T> (BuildContext context) → T? -
Attempts to retrieve the value of type
T
from the nearest ancestor WithValue widget. -
of<
T> (BuildContext context) → T -
Retrieves the value of type
T
from the nearest ancestor WithValue widget.