didAdd method Null safety

  1. @protected
  2. @mustCallSuper
void didAdd()
mustCallSuper">@mustCallSuperprotected">@protected

Called after install when the route is added to the navigator.

This method is called instead of didPush when the route immediately appears on screen without any push transition.

The didChangeNext and didChangePrevious methods are typically called immediately after this method is called.

Implementation

@protected
@mustCallSuper
void didAdd() {
  if (navigator?.widget.requestFocus ?? false) {
    // This TickerFuture serves two purposes. First, we want to make sure
    // that animations triggered by other operations will finish before focusing the
    // navigator. Second, navigator.focusScopeNode might acquire more focused
    // children in Route.install asynchronously. This TickerFuture will wait for
    // it to finish first.
    //
    // The later case can be found when subclasses manage their own focus scopes.
    // For example, ModalRoute creates a focus scope in its overlay entries. The
    // focused child can only be attached to navigator after initState which
    // will be guarded by the asynchronous gap.
    TickerFuture.complete().then<void>((void _) {
      // The route can be disposed before the ticker future completes. This can
      // happen when the navigator is under a TabView that warps from one tab to
      // another, non-adjacent tab, with an animation. The TabView reorders its
      // children before and after the warping completes, and that causes its
      // children to be built and disposed within the same frame. If one of its
      // children contains a navigator, the routes in that navigator are also
      // added and disposed within that frame.
      //
      // Since the reference to the navigator will be set to null after it is
      // disposed, we have to do a null-safe operation in case that happens
      // within the same frame when it is added.
      navigator?.focusScopeNode.requestFocus();
    });
  }
}