Page class

Marks a class as a page that will be registered with the Spark router.

Pages are the entry points for your application's routes. Each page class must extend SparkPage and will be automatically registered with the generated router.

Usage

@Page(path: '/users/:id')
class UserPage extends SparkPage<User> {
  @override
  Future<PageResponse<User>> loader(PageRequest request) async {
    final userId = request.pathParamInt('id');
    final user = await fetchUser(userId);
    if (user == null) return PageRedirect('/404');
    return PageData(user);
  }

  @override
  String render(User data, PageRequest request) {
    return '<h1>${data.name}</h1>';
  }
}

Path Parameters

Use :paramName syntax for path parameters:

  • /users/:id - Single parameter
  • /posts/:postId/comments/:commentId - Multiple parameters

Parameters are available via PageRequest.pathParams.

Constructors

Page({required String path, List<String> methods = const ['GET']})
Creates a page annotation with the given path.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
methods List<String>
HTTP methods this page responds to.
final
path String
The URL path pattern for this page.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

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