site Logo
Home
Portfolio
Python
JavaScript
AI
Search
How to Create and Publish a Flutter Package to pub.dev
Flutter
Dart
Mobile

How to Create and Publish a Flutter Package to pub.dev

A step-by-step guide to creating a Flutter package, testing it locally, and publishing it to pub.dev.

July 19, 2024
3 minutes

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

  1. Create a New Package: Use the Flutter CLI to create a new package:

    Replace my_flutter_package with your package name.

  2. Understand the File Structure:

    • lib: Contains the public API of your package.
    • test: Houses the test files for your package.
  3. Code Your Library: Place your implementation in the lib/src directory, which is the recommended structure. Use a barrel file to export functionalities:

Step 2: Write Documentation

  1. Add a README.md: Describe your package, its purpose, and usage examples.

    Usage

    Import the package:

  2. Create a CHANGELOG.md: Keep track of changes in each version.

  3. 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:

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:

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:

This is useful for sharing the package before publishing it to pub.dev, especially during collaboration or active development.


Step 4: Prepare for Publishing

  1. 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.
  2. Run Pre-Publish Checks: Use:

    This verifies the package for errors or missing fields.

Step 5: Publish to pub.dev

  1. Authenticate: Log in to pub.dev:

  2. Publish: If the dry run is successful, publish your package:

    Follow 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:

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.

Share this Article
Comments are disabled

Table Of Content

Creating and Publishing a Flutter Package to pub.dev
Step 1: Set Up Your Flutter Package
Step 2: Write Documentation
Step 3: Create an Example
Step 4: Prepare for Publishing
Step 5: Publish to pub.dev
Pro Tips for Success

Latest Posts

The Future of Web Development: Build Full-Stack Apps with Bolt.new (No Coding Required!)
November 19, 2024Web-Dev

The Future of Web Development: Build Full-Stack Apps with Bolt.new (No Coding Required!)

Bolt.new revolutionizes web development by letting anyone create full-stack web apps with AI, even without coding experience!

Article
Top 10 VS Code Extensions to Supercharge Your Workflow
March 23, 2024VS Code

Top 10 VS Code Extensions to Supercharge Your Workflow

Boost your productivity and streamline your development with these essential VS Code extensions.

Article
ModernBERT: A Leap Forward in Long-Context Language Models
December 19, 2024NLP

ModernBERT: A Leap Forward in Long-Context Language Models

An overview of ModernBERT, a new BERT-style model with long-context capabilities and superior performance across various tasks.

Article
site Logo
  • About
  • Privacy Policy
  • Contact
© 2026 Seyf ELislam. All Rights Reserved.
Developed byseyf1elislam|TechTuneDz Team
1
flutter create --template=package my_flutter_package
1
flutter create example
1
flutter pub publish --dry-run
1
flutter pub login
1
flutter pub publish
1
library my_flutter_package;
2
3
export 'src/my_feature.dart';
1
import 'package:my_flutter_package/my_flutter_package.dart';
1
dependencies:
2
my_flutter_package:
3
path: ../
1
dependencies:
2
my_flutter_package:
3
git:
4
url: https://github.com/username/my_flutter_package.git
5
ref: main # Optional, specifies the branch or commit
1
dependencies:
2
my_flutter_package: ^1.0.0
1
# My Flutter Package
2
A package to achieve [feature description].
3
## Installation
4
Add this to your `pubspec.yaml`:
5
```yaml
6
dependencies:
7
my_flutter_package: ^1.0.0