loader abstract method
Loads data for this page.
This method is called before rendering and should return either:
- PageData with the typed data to pass to render
- PageRedirect to redirect to another page
- PageError to display an error page
The loader can perform async operations like fetching from APIs, reading from databases, making HTTP requests, etc.
Example
@override
Future<PageResponse<User>> loader(PageRequest request) async {
final userId = request.pathParamInt('id');
// Fetch from external API
final response = await http.get(Uri.parse('$apiUrl/users/$userId'));
if (response.statusCode == 404) {
return PageRedirect('/404');
}
final user = User.fromJson(jsonDecode(response.body));
return PageData(user);
}
Implementation
Future<PageResponse<T>> loader(PageRequest request);