setConfiguration method

void setConfiguration(
  1. PageStackConfiguration configuration, {
  2. PageStackMatchMode mode = PageStackMatchMode.keyOrNullPathNoGap,
  3. @internal PAbstractPageStackConfigurationSetter<PagePath>? setter,
  4. bool fire = true,
})

Recovers pages and their states.

  1. Compares the current stack with given page states from bottom to top. For each page with key matching the path's key, recovers its state.

  2. When a mismatch is found, pops and disposes the remaining pages. This goes from top to bottom.

  3. For page states that were not matched, creates the pages with createPage factory and sets their states. Stops at the first page that failed to be created.

Implementation

void setConfiguration(
  PageStackConfiguration configuration, {
  PageStackMatchMode mode = PageStackMatchMode.keyOrNullPathNoGap,
  @internal PAbstractPageStackConfigurationSetter? setter,
  bool fire = true,
}) {
  final useSetter = setter ?? _getConfigurationSetter(mode);
  final oldPages = [..._pages];

  useSetter.set(
    pages: _pages,
    configuration: configuration,
    createAndPushPage: _createAndPushPage,
  );

  if (_pages.isEmpty) {
    _pages.addAll(oldPages);

    throw Exception(
      'PageStack was about to be emptied by setting '
      'a configuration state. '
      'The stack should never be empty according to the Navigator API. '
      'Most likely your page factory failed to create a page by a key.',
    );
  }

  final newPageSet = {for (final page in _pages) page};

  for (int i = oldPages.length; --i >= 0;) {
    final page = oldPages[i];

    if (newPageSet.contains(page)) {
      continue;
    }

    handleRemoved(
      oldPages.elementAtOrNullIncludingNegative(i - 1),
      page,
      page.createPopEvent(data: null, cause: PopCause.diff),
    );
  }
}