currentState property Null safety

T? currentState

The State for the widget in the tree that currently has this global key.

The current state is null if (1) there is no widget in the tree that matches this global key, (2) that widget is not a StatefulWidget, or the associated State object is not a subtype of T.

Implementation

T? get currentState {
  final Element? element = _currentElement;
  if (element is StatefulElement) {
    final StatefulElement statefulElement = element;
    final State state = statefulElement.state;
    if (state is T)
      return state;
  }
  return null;
}