SparkPage<T> class
abstract
Abstract base class for Spark pages.
Pages are the entry points for your application's routes. Each page must implement loader to fetch data and render to produce HTML.
The generic type T represents the data type returned by your loader.
This provides type safety between your loader and render methods.
Basic 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>
<p>${data.email}</p>
''';
}
}
Pages Without Data
For pages that don't need to load data, use void as the type:
@Page(path: '/')
class HomePage extends SparkPage<void> {
@override
Future<PageResponse<void>> loader(PageRequest request) async {
return PageData(null);
}
@override
String render(void data, PageRequest request) {
return '<h1>Welcome!</h1>';
}
}
With Components (Islands)
@Page(path: '/counter')
class CounterPage extends SparkPage<void> {
@override
List<ComponentInfo> get components => [
ComponentInfo('my-counter', Counter.new),
];
@override
Future<PageResponse<void>> loader(PageRequest request) async {
return PageData(null);
}
@override
String render(void data, PageRequest request) {
return Counter(start: 0).render();
}
}
Constructors
Properties
-
additionalScripts
→ List<
String> -
Additional scripts to load.
no setter
-
components
→ List<
ComponentInfo> -
Returns the list of island components used by this page.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- headContent → Object?
-
Additional content for the HTML
<head>element.no setter - inlineStyles → Stylesheet?
-
Inline CSS styles for this page.
no setter
- lang → String
-
The language attribute for the HTML document.
no setter
-
middleware
→ List<
Middleware> -
Middleware to apply to this page's route.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stylesheets
→ List<
String> -
Additional stylesheets to load.
no setter
Methods
-
loader(
PageRequest request) → Future< PageResponse< T> > - Loads data for this page.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
render(
T data, PageRequest request) → Node - Renders the page HTML content.
-
title(
T data, PageRequest request) → String - Returns the page title.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited