Creating and publishing a Dart/Flutter package to
pub.dev
allows you to share your code with the community. This guide will take you through the entire process step-by-step—from creating a package project to publishing it.Step 1: Create a New Package
Using the Dart Command Line
Run the following command to create a new package:
dart create -t package-simple your_package_name
Replace your_package_name
with the name of your package. In my case tarsier_logger is the name of my package. This command generates a new Dart package with a basic structure.
Using the Flutter Command Line (if it's a Flutter package)
For Flutter-specific packages, use:
flutter create --template=package your_package_name
Folder Structure
Your package folder will look like this:
your_package_name/ ├── lib/ │ └── your_package_name.dart ├── test/ ├── pubspec.yaml ├── README.md ├── CHANGELOG.md ├── LICENSE
Step 2: Implement Your Package Code
Edit the Main Library File
Open the lib/your_package_name.dart
file and add your functionality. For example:
library your_package_name; /// A simple utility to greet users. class Greeter { final String name; Greeter(this.name); /// Returns a greeting message. String greet() => 'Hello, $name!'; }
Step 3: Add Metadata in pubspec.yaml
Edit the pubspec.yaml
file to include essential metadata:
name: your_package_name description: A Dart/Flutter package for greeting users. version: 1.0.0 homepage: https://github.com/yourusername/your_package_name environment: sdk: ">=2.18.0 <3.0.0" dependencies: # Add package dependencies here. dev_dependencies: test: ^1.22.0
Key fields:
name
: Unique package name.description
: A short summary of what your package does.version
: Start with1.0.0
for new packages.environment.sdk
: Defines the compatible Dart SDK versions.
Step 4: Add Documentation
Create a README.md
File
Write a clear and concise README.md
that describes your package. Include usage examples, like this:
# your_package_name A Dart/Flutter package for greeting users. ## Usage ```dart import 'package:your_package_name/your_package_name.dart'; void main() { var greeter = Greeter('Alice'); print(greeter.greet()); // Output: Hello, Alice! }
Add a CHANGELOG File
Contains the version and its changes. Example:
### Add a `CHANGELOG.md` File Document changes for each version. Example: ```markdown # CHANGELOG ## [1.0.0] - YYYY-MM-DD ### Added - Initial release of the package. - Added `Greeter` class for greeting users.
Add a LICENSE
File
Use a standard open-source license like MIT. Example:
MIT License Copyright (c) 2024 Your Name
Step 5: Test Your Package
Write unit tests in the test
folder. Example:
import 'package:your_package_name/your_package_name.dart'; import 'package:test/test.dart'; void main() { test('Greeter should return correct greeting', () { var greeter = Greeter('Alice'); expect(greeter.greet(), 'Hello, Alice!'); }); }
dart test
Step 6: Prepare for Publishing
Verify Your Package
Run this command to check your package for potential issues:
dart pub publish --dry-run
dart pub publish -n
Fix any errors or warnings, such as missing README.md
, CHANGELOG.md
, or LICENSE
files.
Format Your Code (Optional)
Ensure your code is properly formatted:
dart format .
Step 7: Publish Your Package
Authenticate with pub.dev
If this is your first time publishing, authenticate with pub.dev
using:
dart pub login
Publish Your Package
When ready, publish your package:
dart pub publish
Step 8: Verify on pub.dev
Once published, visit your package page on pub.dev
:
https://pub.dev/packages/your_package_name
Check that:
- The
README.md
renders correctly. - The documentation is clear and includes examples.
- All metadata (description, license, version) is accurate.