Route class

Annotation for handler methods that requests should be routed when using package shelf_router_generator.

The shelf_router_generator packages makes it easy to generate a function that wraps your class and returns a Router that forwards requests to annotated methods. Simply add the shelf_router_generator and build_runner packages to dev_dependencies, write as illustrated in the following example and run pub run build_runner build to generate code.

Example

// Always import 'shelf_router' without 'show' or 'as'.
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart' show Request, Response;

// Include generated code, this assumes current file is 'my_service.dart'.
part 'my_service.g.dart';

class MyService {
  @Route.get('/say-hello/<name>')
  Future<Response> _sayHello(Request request, String name) async {
    return Response.ok('hello $name');
  }

  /// Get a router for this service.
  Router get router => _$MyServiceRouter(this);
}

It is also permitted to annotate public members, the only requirement is that the member has a signature accepted by Router as handler.

Annotations
  • @sealed

Constructors

Route(String verb, String route)
Create an annotation that routes requests matching verb and route to the annotated method.
const
Route.all(String route)
Route all requests matching route to annotated method.
const
Route.connect(String route)
Route CONNECT requests matching route to annotated method.
const
Route.delete(String route)
Route DELETE requests matching route to annotated method.
const
Route.get(String route)
Route GET requests matching route to annotated method.
const
Route.head(String route)
Route HEAD requests matching route to annotated method.
const
Route.mount(String prefix)
Route MOUNT requests matching route to annotated method.
const
Route.options(String route)
Route OPTIONS requests matching route to annotated method.
const
Route.post(String route)
Route POST requests matching route to annotated method.
const
Route.put(String route)
Route PUT requests matching route to annotated method.
const
Route.trace(String route)
Route TRACE requests matching route to annotated method.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
route String
HTTP route for request routed to the annotated method.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
verb String
HTTP verb for requests routed to the annotated method.
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited