Setting Up Shared Dependencies
What you'll learn
- Generating the shared dependencies package with barrel exports
- Choosing your state management solution (Bloc, Provider, or Riverpod)
- How centralized dependencies prevent version conflicts
- Importing shared packages across your monorepo
Prerequisites
- An existing Archipelago monorepo (see Monorepo Scaffolding)
Step 1: Generate the Shared Dependencies
bash
archipelago generate shared_dependenciesYou will be prompted for:
- appName —
MyApp(must match your monorepo app name) - stateManagement —
bloc,provider, orriverpod - isForMonorepo —
true
Or use a config file:
json
{
"appName": "MyApp",
"stateManagement": "bloc",
"isForMonorepo": true
}bash
archipelago generate shared_dependencies --config deps_config.jsonStep 2: Understand the Generated Structure
shared/
└── dependencies/
├── lib/
│ ├── dependencies.dart # Main barrel export
│ └── src/
│ ├── di.dart # GetIt + injectable exports
│ ├── state.dart # State management exports
│ ├── routing.dart # GoRouter exports
│ └── common.dart # Equatable, dartz, etc.
└── pubspec.yaml # All shared dependenciesStep 3: How Barrel Exports Work
Instead of every package declaring its own dependency on get_it, equatable, etc., they all import from one place:
dart
// In any feature package
import 'package:dependencies/dependencies.dart';
// This gives you access to:
// - GetIt, Injectable (DI)
// - Bloc/Cubit or Provider or Riverpod (state management)
// - GoRouter (routing)
// - Equatable, dartz (common utilities)Step 4: Add the Dependency
In each feature's pubspec.yaml:
yaml
dependencies:
dependencies:
path: ../../shared/dependenciesStep 5: State Management Choice
The stateManagement variable determines which packages are included:
| Choice | Packages Included |
|---|---|
bloc | flutter_bloc, bloc, bloc_concurrency |
provider | provider |
riverpod | flutter_riverpod, riverpod_annotation |
This is set once at generation time. All features in your monorepo use the same state management solution.
Step 6: Adding New Shared Dependencies
When you need a new package available across features, add it to the shared dependencies package:
yaml
# shared/dependencies/pubspec.yaml
dependencies:
freezed_annotation: ^2.4.0
json_annotation: ^4.8.0Then re-export it:
dart
// shared/dependencies/lib/src/common.dart
export 'package:freezed_annotation/freezed_annotation.dart';
export 'package:json_annotation/json_annotation.dart';Common Customizations
| Customization | Where to Change |
|---|---|
| Add a shared package | pubspec.yaml + barrel export file |
| Change state management | Regenerate with different stateManagement |
| Add category exports | Create new file in lib/src/, export from barrel |
| Pin versions | Lock versions in pubspec.yaml |
Next Steps
- Set up Shared App Config for environment variables
- Generate your first feature using shared deps