Structuring Real Flutter Apps
Organise a production Flutter app with Riverpod or Bloc, go_router, repositories, code generation and key pub.dev packages.
Intermediate · about 10 hours · difficulty 3/5
Learn first: Widget Tree, BuildContext & Lifecycle, App-wide State Tools
A demo app fits in main.dart; a real one needs layers so you can test logic and change data sources without touching UI.
A common layered structure
- UI layer: widgets and screens, reading state from providers or blocs.
- State layer: Riverpod
Notifier/AsyncNotifieror Bloc/Cubit classes holding screen state and business rules. - Data layer: repositories that hide where data comes from (HTTP, database, cache).
- Organise folders by feature (
features/auth,features/feed) rather than by type once the app grows.
Useful packages
- go_router for declarative routing and deep links.
- dio or
httpfor networking. - freezed and json_serializable for immutable models and JSON parsing via code generation (
dart run build_runner build). - drift or sqflite for SQLite, shared_preferences for simple settings.
Pitfalls
- Business logic inside
buildmethods cannot be unit tested. - Mixing several state management approaches in one app confuses everyone. Pick one per project.
Good enough to move on: you have a two-feature app where a repository is injected into a notifier or bloc and replaced by a fake in tests.
Key concepts
- Layered architecture
- Feature-first folders
- Riverpod providers and notifiers
- Bloc and Cubit
- go_router
- freezed and json_serializable
- Dependency injection for tests
Mini project
Build a news reader with a feed and bookmarks feature, using go_router, a repository layer and Riverpod or Bloc, with one unit test per notifier.
Check yourself
What does a repository layer let you do that calling HTTP directly from a widget does not?
Free resources
- Flutter: Guide to app architecture (Flutter)
- Riverpod (Riverpod)
- Bloc library (Bloc)