initState method

  1. @override
void initState ()
override

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 widget used to configure this object (i.e., widget).

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, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

You cannot use BuildContext.inheritFromWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.inheritFromWidgetOfExactType can be used there.

If you override this, make sure your method starts with a call to super.initState().

Implementation

@override
void initState() {
  super.initState();
  for (NavigatorObserver observer in widget.observers) {
    assert(observer.navigator == null);
    observer._navigator = this;
  }
  String initialRouteName = widget.initialRoute ?? Navigator.defaultRouteName;
  if (initialRouteName.startsWith('/') && initialRouteName.length > 1) {
    initialRouteName = initialRouteName.substring(1); // strip leading '/'
    assert(Navigator.defaultRouteName == '/');
    final List<String> plannedInitialRouteNames = <String>[
      Navigator.defaultRouteName,
    ];
    final List<Route<dynamic>> plannedInitialRoutes = <Route<dynamic>>[
      _routeNamed<dynamic>(Navigator.defaultRouteName, allowNull: true, arguments: null),
    ];
    final List<String> routeParts = initialRouteName.split('/');
    if (initialRouteName.isNotEmpty) {
      String routeName = '';
      for (String part in routeParts) {
        routeName += '/$part';
        plannedInitialRouteNames.add(routeName);
        plannedInitialRoutes.add(_routeNamed<dynamic>(routeName, allowNull: true, arguments: null));
      }
    }
    if (plannedInitialRoutes.contains(null)) {
      assert(() {
        FlutterError.reportError(
          FlutterErrorDetails(
            exception:
              'Could not navigate to initial route.\n'
              'The requested route name was: "/$initialRouteName"\n'
              'The following routes were therefore attempted:\n'
              ' * ${plannedInitialRouteNames.join("\n * ")}\n'
              'This resulted in the following objects:\n'
              ' * ${plannedInitialRoutes.join("\n * ")}\n'
              'One or more of those objects was null, and therefore the initial route specified will be '
              'ignored and "${Navigator.defaultRouteName}" will be used instead.'
          ),
        );
        return true;
      }());
      push(_routeNamed<Object>(Navigator.defaultRouteName, arguments: null));
    } else {
      plannedInitialRoutes.forEach(push);
    }
  } else {
    Route<Object> route;
    if (initialRouteName != Navigator.defaultRouteName)
      route = _routeNamed<Object>(initialRouteName, allowNull: true, arguments: null);
    route ??= _routeNamed<Object>(Navigator.defaultRouteName, arguments: null);
    push(route);
  }
  for (Route<dynamic> route in _history)
    _initialOverlayEntries.addAll(route.overlayEntries);
}