Shared Packages
How to keep Flutter dependency versions in one shared package
Free Reference for shared_dependencies: every variable, option and package it generates. Command: archipelago generate shared_dependencies
Generate the shared dependencies package, choose a state management solution, and import shared packages across your monorepo without version conflicts.
What you'll learn
- Generating the shared dependencies package with barrel exports
- Choosing your state-management solution
- Seeing how centralized dependencies prevent version conflicts
- Importing shared packages across your monorepo
Prerequisites
- An existing Archipelago monorepo (see Monorepo Scaffolding)
New projects already include this brick.
Step 1: Generate the Shared Dependencies
archipelago generate shared_dependenciesThe prompts are:
- appName: your app name, for example
MyApp - stateManagement:
bloc(default),cubit,providerorriverpod - isForMonorepo: leave this as
true
Or use a config file:
{
"appName": "MyApp",
"stateManagement": "bloc",
"isForMonorepo": true
}archipelago generate shared_dependencies --config deps_config.jsonStep 2: Understand the Generated Structure
shared/dependencies/
├── lib/
│ ├── dependencies.dart # Umbrella barrel
│ ├── auto_route.dart # auto_route re-exports
│ ├── di.dart # get_it and injectable re-exports
│ ├── equality.dart # equatable re-exports
│ ├── l10n.dart # slang and slang_flutter re-exports
│ ├── network.dart # dio re-export, not in the umbrella barrel
│ └── state.dart # State management re-exports
└── pubspec.yaml # Pins the shared versionsStep 3: How Barrel Exports Work
Packages import shared libraries from one place:
import 'package:dependencies/dependencies.dart';
import 'package:dependencies/di.dart';The umbrella file exports routing, DI, equality, localization and state. It does not export network.dart. Import it directly where you need dio:
import 'package:dependencies/network.dart';Step 4: Add the Dependency
In each package that needs it:
dependencies:
dependencies:
path: ../../shared/dependenciesStep 5: State Management Choice
stateManagement decides what state.dart exports:
| Choice | Exports |
|---|---|
bloc or cubit | flutter_bloc |
provider | provider and ChangeNotifier |
riverpod | flutter_riverpod and riverpod_annotation |
The choice is set at generation. Every feature in the monorepo uses the same choice. Changing it later regenerates the exports. It does not migrate feature code already written.
Step 6: Add a New Shared Dependency
Add the package to the shared pubspec.yaml, then export it from a barrel file:
# shared/dependencies/pubspec.yaml
dependencies:
freezed_annotation: ^4.0.1// shared/dependencies/lib/equality.dart
export 'package:freezed_annotation/freezed_annotation.dart';Common Customizations
| Customization | Where to Change |
|---|---|
| Add a shared package | pubspec.yaml and a barrel file |
| Add a category | A new file in lib/, exported from dependencies.dart |
| Pin versions | pubspec.yaml |
Next Steps
- Set up Shared App Config for environment variables
- Generate your first feature using shared dependencies