CodeWithAndrea
Scalable App Architecture -This scalable app architecture design is inspired by Andrea Bizzotto. Full documentation of this design can be found here. In addition to that, you can use a VSCode extension called Flutter Feature Scaffolding for easily generating the features' folders. This design is using BLoC state-management solution, and for more information on BLoC library, I would strongly suggest you to follow their official documentation.
This project contains everything that you will need in order to immediately start developing your highly scaled application. You can also call this folder structure as Feature-first (layers inside features) design.
About the folder structure
lib/
โโโ src/
โ โโโ common_widgets/
โ โ โโโ common_button.dart
โ โโโ constants/
โ โ โโโ colors.dart
โ โโโ exceptions/
โ โ โโโ routing_exception.dart
โ โโโ features/
โ โ โโโ address/
โ โ โ โโโ data/
โ โ โ โ โโโ datasources/
โ โ โ โ โ โโโ address_datasources.dart
โ โ โ โ โโโ models/
โ โ โ โ โ โโโ address_model.dart
โ โ โ โ โโโ repositories/
โ โ โ โ โโโ address_repositories.dart
โ โ โ โโโ domain/
โ โ โ โ โโโ entities/
โ โ โ โ โ โโโ address_entities.dart
โ โ โ โ โโโ repositories/
โ โ โ โ โ โโโ address_repositories.dart
โ โ โ โ โโโ usecases/
โ โ โ โ โโโ address_usecases.dart
โ โ โ โโโ presentation/
โ โ โ โโโ bloc/
โ โ โ โ โโโ address_bloc.dart
โ โ โ โ โโโ address_event.dart
โ โ โ โ โโโ address_state.dart
โ โ โ โโโ pages/
โ โ โ โ โโโ address_page.dart
โ โ โ โโโ widgets/
โ โ โ โโโ address_widgets.dart
โ โ โโโ authentication/
โ โ โ โโโ (...)
โ โ โโโ cart/
โ โ โ โโโ (...)
โ โ โโโ checkout/
โ โ โ โโโ (...)
โ โ โโโ orders/
โ โ โ โโโ (...)
โ โ โโโ products/
โ โ โโโ (...)
โ โโโ localization/
โ โ โโโ app_bn.arb
โ โ โโโ app_en.arb
โ โ โโโ localization.dart
โ โโโ routing/
โ โ โโโ routing.dart
โ โโโ utils/
โ โโโ utils.dart
โโโ main.dart
You might notice that some files are just dummy (like api.dart, model.dart). They are placed there just for git to take the folder structure into consideration. Leaving those folders empty won't let git to take them into consideration. Since this is mostly a startup project, I was thinking of offering you the standard folder structure from the start. Of course, this may change multiple times, improving every time more and more.
Why this design pattern?
This architecture is made of four distinct layers, each containing the components that our app needs:
โฎ presentation: widgets, states, and controllers
โฎ application: services
โฎ domain: models
โฎ data: repositories, data sources,
and DTOs (data transfer objects)
Of course, if we're building just a single-page app, we can put all files in one folder and call it a day. ๐
But as soon as we start adding more pages and have various data models to deal with, how can we organize all our files in a consistent way?
In practice, a feature-first or layer-first approach is often used.
Layer-first: Drawbacks
This approach is easy to use in practice, but doesn't scale very well as the app grows.
For any given feature, files that belong to different layers are far away from each other. And this makes it harder to work on individual features because we have to keep jumping to different parts of the project.
And if we decide that we want to delete a feature, it's far too easy to forget certain files, because they are all organized by layer.
For these reasons, the feature-first approach is often a better choice when building medium/large apps.
What about shared code?
Of course, when building real apps you'll find that your code doesn't always fit neatly into specific folders as you intended.
What if two or more separate features need to share some widgets or model classes?
In these cases, it's easy to end up with folders called shared
or common
, or utils
.
But how should these folders themselves be organized? And how do you prevent them from becoming a dumping ground for all sorts of files?
If your app has 20 features and has some code that needs to be shared by only two of them, should it really belong to a top-level shared
folder?
What if it's shared among 5 features? Or 10?
In this scenario, there is no right or wrong answer, and you have to use your best judgement on a case-by-case basis.
feature
?
What is a
Feature-first is not about the UI!
Feature is not about what the user sees, but what the user does:
โฎ authenticate
โฎ manage the shopping cart
โฎ checkout
โฎ view all past orders
โฎ leave a review
In other words, a feature is a functional requirement that helps the user complete a given task.
And using some hints from domain-driven design, I decided to organize the project structure around the domain layer.
Once I figured that out, everything fell into place. And I ended up with seven functional areas that you can see on the right.
Note that with this approach is still possible for code inside a given feature to depend on code from a different feature. For example:
โฎ the product page shows a list of reviews
โฎ the orders page shows some product information
โฎ the checkout flow requires the user to authenticate first
But we end up with far fewer files that are shared across all features, and the entire structure is much more balanced.
feature-first
, the right way?
How to do In summary, the feature-first approach lets us structure our project around the functional requirements of our app.
So here's how to use this correctly in your own apps:
โ start from the domain layer and identify the model classes and business logic for manipulating them
โ create a folder for each model (or group of models) that belong together
โ within that folder, create the presentation
, application
, domain
, data
sub-folders as needed
โ inside each sub-folder, add all the files you need