Skip to content
Back to archipelago.banuacoder.com

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 ​

New projects already include this brick.

Step 1: Generate the Shared Dependencies ​

bash
archipelago generate shared_dependencies

The prompts are:

  • appName: your app name, for example MyApp
  • stateManagement: bloc (default), cubit, provider or riverpod
  • isForMonorepo: leave this as true

Or use a config file:

json
{
  "appName": "MyApp",
  "stateManagement": "bloc",
  "isForMonorepo": true
}
bash
archipelago generate shared_dependencies --config deps_config.json

Step 2: Understand the Generated Structure ​

text
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 versions

Step 3: How Barrel Exports Work ​

Packages import shared libraries from one place:

dart
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:

dart
import 'package:dependencies/network.dart';

Step 4: Add the Dependency ​

In each package that needs it:

yaml
dependencies:
  dependencies:
    path: ../../shared/dependencies

Step 5: State Management Choice ​

stateManagement decides what state.dart exports:

ChoiceExports
bloc or cubitflutter_bloc
providerprovider and ChangeNotifier
riverpodflutter_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:

yaml
# shared/dependencies/pubspec.yaml
dependencies:
  freezed_annotation: ^4.0.1
dart
// shared/dependencies/lib/equality.dart
export 'package:freezed_annotation/freezed_annotation.dart';

Common Customizations ​

CustomizationWhere to Change
Add a shared packagepubspec.yaml and a barrel file
Add a categoryA new file in lib/, exported from dependencies.dart
Pin versionspubspec.yaml

Next Steps ​