back<T extends Object?> method

Future<void> back<T extends Object?>([
  1. T? result
])

Requests the wizard to show the previous page. Optionally, result can be returned to the previous page.

Implementation

Future<void> back<T extends Object?>([T? result]) async {
  if (state.length <= 1) {
    throw WizardException(
        '`Wizard.back()` called from the first route ${state.last.name}');
  }

  // go back to a specific route, or pick the previous route on the list
  final previous = await routes[currentRoute]!.onBack?.call(state.last);
  if (previous != null) {
    if (!routes.keys.contains(previous)) {
      throw WizardException(
          '`Wizard.routes` is missing route \'$previous\'.');
    }
  }

  final start = previous != null
      ? state.lastIndexWhere((settings) => settings.name == previous) + 1
      : state.length - 1;

  _updateState((state) {
    final copy = List<WizardRouteSettings>.of(state);
    copy[start].completer.complete(result);
    return copy..replaceRange(start, state.length, []);
  });
}