This project extends the task API with a portable model contract that a Flutter client can read. Share the wire model and parser, not database access, server secrets or widget state. Deployment versions can differ even inside a monorepo.
Create the package boundary
Use a workspace with contracts, api and a client package. The contracts library imports only portable Dart libraries. The API depends on it alongside Shelf; Flutter depends on it alongside its UI/runtime packages.
class TaskDto {
const TaskDto({required this.id, required this.title, required this.done});
final int id;
final String title;
final bool done;
factory TaskDto.fromJson(Object? input) {
if (input case {'id': int id, 'title': String title, 'done': bool done}
when id > 0 && title.trim().isNotEmpty) {
return TaskDto(id: id, title: title, done: done);
}
throw const FormatException('Invalid task response.');
}
Map<String, Object> toJson() => {'id': id, 'title': title, 'done': done};
}
This is the shared library’s model, not a complete application. Move it into a public package entry point and add direct parsing tests. Keep API create/patch request models separate from a response model when clients should not set server-owned fields.
Connect a client deliberately
The client repository owns an HTTP client, checks status codes and validates the response envelope before calling TaskDto.fromJson for each item. A Flutter screen then renders loading, empty, success and failure states without parsing dynamic JSON inside build.
Preserve independent responsibilities
The server still verifies the authenticated owner and validates writes. A mobile client having the same class does not make its input trustworthy. The contracts package should not import dart:io, a PostgreSQL driver or Flutter widgets if it must also build for the browser.
Test compatibility
Test an older response missing a newly optional field, an unknown enum value and extra fields. Decide whether each is accepted or rejected. Make breaking API changes deliberately rather than assuming every client updates at the same instant as the server.
Exercise
Refactor the task starter to use this shared response model while keeping its HTTP tests unchanged. Add one optional field with a backward-compatible parser. Verify both the server target and the intended Flutter client target separately.