What is modularization in Flutter and how does melos help?
TL;DR: Modularization splits a Flutter app into multiple Dart packages within a monorepo. Each feature (quiz, profile) is a separate package with its own pubspec.yaml. Melos manages the monorepo: versioning, bootstrapping, running scripts across all packages.
Full Answer
Large apps suffer from slow build times and tight coupling when everything is in one package. Modularization solves this by giving each feature its own isolated Dart package.
Package Structure
- ▸packages/quiz_feature: Quiz UI + bloc + models
- ▸packages/questions_api: Remote data source (Dio client)
- ▸packages/questions_repository: Repository interface + implementation
- ▸packages/shared_ui: Design system, shared widgets
- ▸apps/mobile: Shell app that composes all packages
The shell app (apps/mobile) depends on all feature packages. Feature packages depend on shared packages but never on other feature packages. This prevents circular dependencies.
Code Examples
# Run all tests: melos run test:all # Runs in parallel across all packages with test/ directories
Common Mistakes
- ✗Creating packages too early — modularize when you feel real coupling pain, not upfront
- ✗Feature packages depending on each other — breaks isolation; use shared packages or events
Interview Tip
Modularization trades build speed for complexity. Mention that module-level caching means only changed modules are rebuilt. Google's internal Flutter teams use this pattern at scale.