match<T extends Object> static method

Route match<T extends Object>(
  1. List<String> methods,
  2. String route,
  3. dynamic controller
)

one route for each method in methods, like ['GET', 'POST']. HEAD comes with GET, so leave it out. An unknown method throws an ArgumentError.

Route.match(<String>['GET', 'POST'], 'contact', controller);

Implementation

static Route match<T extends Object>(
  List<String> methods,
  String route,
  dynamic controller,
) {
  List<HttpRequestMethod> list = <HttpRequestMethod>[];
  for (String method in methods) {
    String upper = method.toUpperCase();
    HttpRequestMethod? found;
    for (HttpRequestMethod value in HttpRequestMethod.values) {
      if (value.name == upper) found = value;
    }
    if (found == null) {
      throw ArgumentError.value(
        method,
        'methods',
        upper == 'HEAD'
            ? 'HEAD comes with GET, leave it out'
            : 'Unknown HTTP method',
      );
    }
    list.add(found);
  }
  return Route()._addMany(list, route, _action<T>(controller));
}