Package
Package - Store the Best of Your Code for Your Projects
Last updated
Package - Store the Best of Your Code for Your Projects
Last updated
Creating and managing packages significantly reduces coding time, allows for code reuse, and makes project maintenance easier. Below are some key benefits of using packages:
Code Reusability
When you create packages for common features or components, you can reuse them across multiple projects without rewriting code. This saves both time and effort.
Easy Management and Maintenance
Packages are well-organized, making them easy to update and maintain. When a feature needs to be changed or upgraded, you can simply update the package without affecting the entire project.
Enhanced Consistency
Using packages ensures that your code remains consistent and follows the same rules and standards across different projects.
Faster Development
With pre-built packages, you can focus on developing project-specific features without spending time on fundamental components.
Collaboration and Sharing
Packages can be shared with the community or within a development team, enhancing collaboration and leveraging collective knowledge.
Reduced Errors
Reusing well-tested and stable packages minimizes the risk of encountering new bugs during application development.
Below is a detailed guide on how to create and manage packages:
I have created a directory named packages to store all the packages I develop.
I categorize them into two types:
The first type belongs to ancestor_core, which consists of foundational packages where I have encapsulated functions following an object-oriented programming style. When using these packages, you simply call them without worrying about the complex internal processing and setup.
The second type consists of UI packages, which contain all the UI-related widgets and components. These help minimize a significant amount of code and can be reused in future projects.
Now, let's dive into one of them: fire_core.
This package stores all the Firebase-related code I have written. In the future, if a new project requires Firebase, I can simply import this package and reuse the previously written code.
pubspec.yaml
analysis_options.yaml
Replace the project's analysis_options.yaml
file to include error analysis configurations from the lint
package.
For package-level analysis_options.yaml
, replace it with:
My idea is to sync all packages using Git, with each package in a project stored in a separate branch. Later, you can merge them all into one if needed.
The idea is to create a packages project on Git and link it to our local packages
directory.
If the directory already exists:
You might find this process a bit tedious, so why not create a separate package project that is independent of the main project?
The benefit of my approach is that when coding, you can easily add, edit, and search within packages while keeping all package code within the main project. This ensures safety in case someone locks your packages on Git.
Step 1: Commit Changes
Step 2 (Optional): Push to Remote
Let's say I have a function to print JSON to the log in a properly formatted way. I will put it inside a class named string.dart
, which is located in a package called dart_core, as shown below:
string.dart
dart_core.dart
pubspec.yaml
And that completes the process of creating and using a function inside a package.
Now, you can store and reuse your best utility functions whenever needed. This method is especially useful for Firebase projects, as you can reuse the setup across multiple projects without having to configure it again.
The lint package helps detect errors in Dart code, ensuring a clean and well-formatted codebase.
To quickly generate the folder and file structure as shown in the images, you can use Mason. If you're not familiar with Mason, check out my brick here:
|