Simon kindly called to my attention, that we can implement the named parameter notFoundHandler (source) with a Handler in the Router constructor, instead of implementing a router.all() (source).


import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;
import 'package:shelf_router/shelf_router.dart';

void main() async {
  final service = Service();

  final server = await shelf_io.serve(service.handler, 'localhost', 8080);
  print('Server running on http://${}:${server.port}');

class Service {
  Handler get handler {
    final router = Router(
      // Instead of using 'router.all', that is commented a few lines down, you can also use 'notFoundAl' as implemented here.
      notFoundHandler: (Request request) => Response.notFound('Page not found');

    router.get('/say-hi/<name>', (Request request, String name) {
      return Response.ok('hi $name');

    // Instead of using 'router.all' we can use the 'notFoundHandler'
    // router.all('/<ignored|.*>', (Request request) {
    //   return Response.notFound('Page not found');
    // });

    return router;