Creating and Publishing a Flutter Package to pub.dev
Flutter packages allow developers to reuse code, share utilities, and enhance app functionality. This guide will walk you through creating a package and publishing it to pub.dev.
Step 1: Set Up Your Flutter Package
-
Create a New Package: Use the Flutter CLI to create a new package:
1flutter create --template=package my_flutter_packageReplace
my_flutter_package
with your package name. -
Understand the File Structure:
lib
: Contains the public API of your package.test
: Houses the test files for your package.
-
Code Your Library: Place your implementation in the
lib/src
directory, which is the recommended structure. Use a barrel file to export functionalities:1library my_flutter_package;23export 'src/my_feature.dart';
Step 2: Write Documentation
-
Add a README.md: Describe your package, its purpose, and usage examples.
1# My Flutter Package2A package to achieve [feature description].3## Installation4Add this to your `pubspec.yaml`:5```yaml6dependencies:7my_flutter_package: ^1.0.0Usage
Import the package:
1import 'package:my_flutter_package/my_flutter_package.dart'; -
Create a CHANGELOG.md: Keep track of changes in each version.
-
License Your Package: Add a LICENSE file. MIT is a common choice, but you can use choosealicense.com for guidance.
Step 3: Create an Example
Build an example app to demonstrate your package. Run:
1flutter create example
This creates a sample Flutter app in an example
folder. Configure its pubspec.yaml
to use the package Using one of:
1. Using the Package Locally:
When testing your package before publishing, link it locally. Update the pubspec.yaml
in the example
app:
1dependencies:2my_flutter_package:3path: ../
This links the package to the directory where your package code resides.
2. Using the Package from Git:
If your package is hosted in a Git repository, specify the repository URL and branch in pubspec.yaml
:
1dependencies:2my_flutter_package:3git:4url: https://github.com/username/my_flutter_package.git5ref: main # Optional, specifies the branch or commit
This is useful for sharing the package before publishing it to pub.dev, especially during collaboration or active development.
Step 4: Prepare for Publishing
-
Update
pubspec.yaml
: Include metadata such as:- Name: Unique package name.
- Version: Follow semantic versioning (e.g.,
1.0.0
). - Description: Briefly describe your package.
- Homepage/Repository: Links to your documentation or GitHub repo.
-
Run Pre-Publish Checks: Use:
1flutter pub publish --dry-runThis verifies the package for errors or missing fields.
Step 5: Publish to pub.dev
-
Authenticate: Log in to pub.dev:
1flutter pub login -
Publish: If the dry run is successful, publish your package:
1flutter pub publishFollow the prompts to complete the process. Once published, the package will be available on pub.dev.
After publishing your package to pub.dev, you can reference it directly by its name and version:
1dependencies:2my_flutter_package: ^1.0.0
The version specifier ^1.0.0
allows upgrades to newer non-breaking versions (e.g., 1.0.1
, 1.1.0
).
Pro Tips for Success
- Maintain Quality: Write comprehensive tests and ensure null safety.
- Follow Best Practices: Organize your code properly (e.g., use
lib/src
for implementation). - Engage the Community: Update documentation, respond to user feedback, and provide regular updates.
This structured approach ensures that your package is not only functional but also well-received by the Flutter community.