grab method
Returns the value
of the ValueListenable that this method was
called on, and starts listening for changes in the ValueListenable
to rebuild the widget associated with the provided BuildContext
when there is a change in the value
of the ValueListenable
.
A Grab is necessary as an ancestor of the widget this method is used for. GrabMissingError is thrown otherwise.
void main() {
runApp(
const Grab(child: ...),
);
}
class Item {
Item({required this.name, required this.quantity});
final String name;
final int quantity;
}
final notifier = ValueNotifier(
Item(name: 'Milk', quantity: 3),
);
...
class InventoryItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
final item = notifier.grab(context);
return Text(item.name);
}
}
Implementation
R grab(BuildContext context) {
return grabAt(context, (value) => value);
}