initState method
Called when this object is inserted into the tree.
The framework will call this method exactly once for each State
object
it creates.
Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the component used to configure this object (i.e., component).
If a State
's build method depends on an object that can itself
change state, for example a ChangeNotifier
or Stream, or some
other object to which one can subscribe to receive notifications, then
be sure to subscribe and unsubscribe properly in initState,
didUpdateComponent, and dispose:
- In initState, subscribe to the object.
- In didUpdateComponent unsubscribe from the old object and subscribe to the new one if the updated component configuration requires replacing the object.
- In dispose, unsubscribe from the object.
You cannot use BuildContext.dependOnInheritedComponentOfExactType
from this
method. However, didChangeDependencies will be called immediately
following this method, and BuildContext.dependOnInheritedComponentOfExactType
can
be used there.
Implementations of this method should start with a call to the inherited
method, as in super.initState()
.
Implementation
@override
void initState() {
super.initState();
PlatformRouter.instance.history.init(context.binding, onChangeState: (state, {url}) {
_update(url ?? context.binding.currentUri.toString(), extra: state, updateHistory: false, replace: true);
});
if (_matchList == null) {
assert(context.binding.isClient);
initRoutes().then((_) {
setState(() {});
});
}
}