go<T> method

Future<StackRouteNavigationResult?> go<T>(
  1. String name, {
  2. dynamic params,
  3. bool forResult = false,
})

route manage apis go

  • T : result type

Implementation

/// [go]
/// * T : result type
Future<StackRouteNavigationResult?> go<T>(String name, {dynamic params, bool forResult = false}) async {
  final index = _descriptors.indexWhere((e) => e.name == name);
  if (index == -1) {
    _log('No Page Descriptor Matched :$name, Navigation Action Ignored');
    return null;
  }
  final curRoute = _routes.last;

  if (forResult) {
    curRoute.completer = Completer<dynamic>();
  }

  /// notify current route that its gonna lose focus
  curRoute.controller?.onFocused.call(false);

  final tarRoute = _buildRoute(
    _descriptors[index],
    params: params,
    forResult: forResult,
  );

  /// emit global navigation event [GBOnStackRouteWillChange]
  GBOnStackRouteWillChange(
    data: GBOnStackRouteWillChangeData(
      routeId: curRoute.id,
      routeName: curRoute.name,
      targetRouteId: tarRoute.id,
      targetRouteName: tarRoute.name,
      type: StackRouteChangeType.push,
      isInitialRoute: false,
    ),
  ).emit();

  if (_index == 0) {
    /// emit global navigation event [GBOnStackRouteIsInitial]
    GBOnStackRouteIsInitial(data: false).emit();
  }

  _routes.add(tarRoute);

  /// request rebuild navigator
  notifyListeners();

  /// after [notifyListeners], the navigator will be rebuild
  /// if navigation result required、wait for result
  ///
  if (forResult) {
    final result = await curRoute.completer!.future;
    if (result is StackRoutePoppedUnexpectedlyException) {
      return StackRouteNavigationResult(isCanceled: true);
    }
    if (result is T) {
      return StackRouteNavigationResult<T>(result: result);
    } else {
      return StackRouteNavigationResult(result: result);
    }
  }
  return null;
}