Package-based architecture — Let’s deliver the packages 📦
We have all seen a lot of architectures like MVVM, MVC, Clean Architecture, etc. But do you know, there’s also a hidden gem in Flutter called local packages that help you keep your code cleaner?
No application is complete without services like APIs, Notification, etc. Where do you keep these codes? Inside the lib folder? Fair enough, almost all architecture has a section for services which are independent of the UI.
But, in Package-based Architecture, we move these services inside a local package so that it becomes independent of your entire application. For example, Firebase Auth functionality is totally independent of any project, the code for login with email & password, Google login, and Apple login, all remain the same throughout multiple projects. Then why not reuse them and make them independent of the project?
So in a Package-based Architecture, you migrate your services into local packages so that they are completely de-coupled from the UI. Then you import these packages into your application and can use it 🤩
Let’s check it out with an example!
Let’s suppose you have an auth service that has the following structure. No matter what code you have inside, we won’t be focusing on that. We will just discuss how we can move this auth service into a package so that we can have Package-based Architecture.
This folder right now is inside the lib folder. Let’s now move it into a package! First you’ll need to create a folder packages
inside the root of your project.
Now go to the location of that folder from the terminal.
Now, to create a new flutter package, run the following command:
flutter create --template=package auth_service
This will create a package named auth_service
inside the packages
folder. The structure will look something like this:
Now you can move all your code to the lib folder of this package and also add the required packages like firebase_core
and firebase_auth
in your package.
You can use auth_service.dart
as the export file so that only one file needs to be imported when you want to use this package. You can add the following code in auth_service.dart
export 'src/firebase_auth_service.dart';
export 'src/models/user_model.dart';
Now, to use this package, you can add the package as follows inside your dependencies of your pubspec.yaml
file:
dependencies:
auth_service:
path: packages/auth_service
Hence, now, whenever you want to have firebase_auth again in different project, you can use this package again. You can either copy-paste the entire folder or host this package separately on git and use it everywhere!
Not just services, you can make local packages for your UI related components like theme, fonts, colors, common widgets, etc. Whatever part of code you think can be made independent, you can convert it to a package 📦
This is a very short article explaining Package-based Architecture.
For reference, you can use https://github.com/AbhishekDoshi26/currency where I have used Package-based Architecture along with Feature-driven Architecture.
Hope you enjoyed this article!
Doubts? Feel free to drop a message @AbhishekDoshi26
Checkout abhishekdoshi.dev for more info 💙
Don’t stop, until you are breathing!💙
- Abhishek Doshi