routeNotFound property
Response
routeNotFound
final
Sentinel Response
object indicating that no matching route was found.
This is the default response value from a Router created without a
notFoundHandler
, when no routes matches the incoming request.
If the routeNotFound object is returned from a Handler
the Router
will consider the route not matched, and attempt to match other routes.
This is useful when mounting nested routers, or when matching a route
is conditioned on properties beyond the path of the URL.
Example
final app = Router();
// The pattern for this route will match '/search' and '/search?q=...',
// but if request does not have `?q=...', then the handler will return
// [Router.routeNotFound] causing the router to attempt further routes.
app.get('/search', (Request request) async {
if (!request.uri.queryParameters.containsKey('q')) {
return Router.routeNotFound;
}
return Response.ok('TODO: make search results');
});
// Same pattern as above
app.get('/search', (Request request) async {
return Response.ok('TODO: return search form');
});
// Create a single nested router we can mount for handling API requests.
final api = Router();
api.get('/version', (Request request) => Response.ok('1'));
// Mounting router under '/api'
app.mount('/api', api);
// If a request matches `/api/...` then the routes in the [api] router
// will be attempted. However, for a request like `/api/hello` there is
// no matching route in the [api] router. Thus, the router will return
// [Router.routeNotFound], which will cause matching to continue.
// Hence, the catch-all route below will be matched, causing a custom 404
// response with message 'nothing found'.
// In the pattern below `<anything|.*>` is on the form `<name|regex>`,
// thus, this simply creates a URL parameter called `anything` which
// matches anything.
app.all('/<anything|.*>', (Request request) {
return Response.notFound('nothing found');
});
Implementation
static final Response routeNotFound = _RouteNotFoundResponse();