initState method

  1. @override
  2. @mustCallSuper
void initState()
inherited

The framework will call this method exactly once. Only when the StateX object is first created.

Implementation

@override
@mustCallSuper
void initState() {
  /// Override this method to perform initialization that depends on the
  /// location at which this object was inserted into the tree.
  /// (i.e. Subscribe to another object it depends on during [initState],
  /// unsubscribe object and subscribe to a new object when it changes in
  /// [didUpdateWidget], and then unsubscribe from the object in [dispose].
  super.initState();

  /// If 'AppState' is not used
  if (rootState == null) {
    /// Registers the given object as a binding observer. Binding
    /// observers are notified when various application events occur,
    /// for example when the system locale changes. Generally, one
    /// widget in the widget tree registers itself as a binding
    /// observer, and converts the system state into inherited widgets.
    WidgetsBinding.instance.addObserver(this);
  }

  /// Become aware of Route changes
  RouteObserverStates.subscribeRoutes(this);

  /// No 'setState()' functions are allowed to fully function at this point.
  _setStateAllowed = false;

  int cnt = 0;
  StateXController con;

  // While loop the active list itself the so additional controllers can be added in a previous initState()
  while (cnt < controllerList.length) {
    con = controllerList[cnt];
    // Add this to the _StateSets Map
    con._addStateToSetter(this);
    con.initState();
    cnt++;
  }

  _setStateAllowed = true;

  /// No 'setState()' functions are necessary
  _setStateRequested = false;

  assert(() {
    if (_printEvents) {
      debugPrint('============ Event: initState() in $this');
    }
    return true;
  }());
}