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:

    1
    flutter create --template=package my_flutter_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:

    1
    library my_flutter_package;
    2
    3
    export 'src/my_feature.dart';

Step 2: Write Documentation

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

    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

    Usage

    Import the package:

    1
    import 'package:my_flutter_package/my_flutter_package.dart';
  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:

1
flutter 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:

1
dependencies:
2
my_flutter_package:
3
path: ../

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:

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

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:

    1
    flutter pub publish --dry-run

    This verifies the package for errors or missing fields.

Step 5: Publish to pub.dev

  1. Authenticate: Log in to pub.dev:

    1
    flutter pub login
  2. Publish: If the dry run is successful, publish your package:

    1
    flutter pub publish

    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:

1
dependencies:
2
my_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.

Share
Comments are disabled