BuildContext.watchId
BuildContext.watchId
is a method similar to BuildContext.watch
, but use id
as first argument to locate the dependency.
Syntax & description
context
: TheBuildContext
object, which provides information about the current widget in the tree.-
T
: The type of the dependency you want to access. These are some points to consider:- If the type is nullable, the method will return
null
if the dependency is not found. - If the type is not nullable, the method will throw an exception if the dependency is not found.
- If the type is nullable, the method will return
id
: An identifier for theT
dependency. The dependency will be located by its type(T
) and theid
.-
listenStates
: An optional function that returns a list of state(RtState
) to listen for changes. If omitted, the method will listen for any changes in theT
dependency.
Usage
This following example demonstrates how to use BuildContext.watchId
:
In this example, the Counter
widget uses BuildContext.watchId
to get the instance of CounterController
by its id
from the nearest ancestor provider(located in main.dart
) and listen for changes in the count
state.