param<T> static method

T? param<T>(
  1. BuildContext context,
  2. String key
)

Implementation

static T? param<T>(BuildContext context, String key) {
  final routeSettings = ModalRoute.of(context)!.settings;
  final argumentsWrapper = (routeSettings.arguments as ArgumentsWrapper);
  final isParamNotRegistered = argumentsWrapper.routeParams == null ||
      !argumentsWrapper.routeParams!.containsKey(key);

  if (isParamNotRegistered) {
    throw ParamNotRegisteredError(
      paramKey: key,
      routeName: routeSettings.name,
    );
  }

  // Check for request paramter type with registered paramter type.
  final paramRegisterdType = argumentsWrapper
      .routeParams![key]!.paramType; // Type with which param was registed
  if (T != paramRegisterdType) {
    AppLogger.instance
      ..warning("========================================")
      ..warning("Mismatching Paramter Type!")
      ..warning(
          "Requested param '$key' with type '$T', but was declared with type '$paramRegisterdType'.\n")
      ..warning(
          "Make sure to pass the type of variable which used when declaring the 'seafarerParam<T>'.")
      ..warning("========================================");
  }

  final defaultParamValue = argumentsWrapper.routeParams![key]!.defaultValue;
  final paramFromNavigationCall =
      argumentsWrapper.params != null ? argumentsWrapper.params![key] : null;
  return (paramFromNavigationCall ?? defaultParamValue) as T?;
}