argsAs<T> method

T argsAs<T>({
  1. T orElse()?,
})

Validates and returns args casted as T

if args is null and orElse is null an error is thrown otherwise orElse is called and args are built by it

Implementation

T argsAs<T>({T Function()? orElse}) {
  final args = _match.args;
  if (args == null) {
    if (orElse == null) {
      final messages = [
        '${T.toString()} can not be null because the corresponding page has a required parameter'
      ];
      if (_match.autoFilled) {
        messages.add(
            '${_match.name} is an auto created ancestor of target route ${_match.flattened.last.name}');
        messages.add(
            'This usually happens when you try to navigate to a route that is inside of a nested-router\nbefore adding the nested-router to the stack first');
        messages.add(
            'try navigating to ${_match.flattened.map((e) => e.name).join(' -> ')}');
      }
      throw MissingRequiredParameterError('\n${messages.join('\n')}\n');
    } else {
      return orElse();
    }
  } else if (args is! T) {
    throw MissingRequiredParameterError(
        'Expected [${T.toString()}],  found [${args.runtimeType}]');
  } else {
    return args;
  }
}