A shared package can keep API model names and parsing rules consistent between a Flutter client and a Dart server. A workspace coordinates dependencies; it does not make every dependency usable on every platform.
Define the workspace
At the repository root, use a workspace-aware pubspec such as:
name: invoice_workspace
publish_to: none
environment:
sdk: ^3.14.0
workspace:
- packages/contracts
- packages/api
Each member has its own package name and SDK constraint and declares resolution: workspace. Run pub resolution from the intended root and inspect the shared lockfile. Follow the
workspace guide
for supported structure and commands.
Keep the shared package portable
The contracts package can hold immutable DTOs, validation rules and a serialization contract. Avoid importing dart:io, Flutter widgets or a database driver into it. The server owns persistence and authorization; the client owns presentation and interaction.
Matching source types do not guarantee a compatible wire contract. Deployment versions can differ, so test missing optional fields, new enum values and old clients. Make additive changes deliberately and retain a compatibility strategy for removed fields.
Verify more than a monorepo build
Analyze and test each member. Build the actual client and backend targets. A package that analyzes under the Dart VM can still import something unsupported by a browser build. Keep generated caches and platform artifacts out of shared source.
Exercise
Move the task API’s JSON contract into a separate pure-Dart package, leaving the in-memory repository on the server. Write a parsing test that passes in both consumers. Explain why sharing a repository implementation with a mobile client would expose the wrong responsibilities.