DEV Community

Cover image for How to Create and Publish a Flutter Package on pub.dev
Mouayed Amr
Mouayed Amr

Posted on

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

Have you ever built a cool feature in Flutter and thought, “Other developers could really use this!”?

That’s when it’s time to turn your code into a Flutter package and share it with the community.

In this guide, I’ll walk you step-by-step through creating your own Flutter package and publishing it on pub.dev
"the official package repository for Dart and Flutter developers"

Step 1: Create a New Package

Open your terminal and run:

flutter create --template=package my_awesome_package

This creates a folder named my_awesome_package with this structure:

my_awesome_package/
├── lib/
│   └── my_awesome_package. Dart
├── pubspec.yaml
└── test/
Enter fullscreen mode Exit fullscreen mode

Tip:
If you want to build a package that includes native Android/iOS code (like accessing sensors or camera), use:

flutter create --template=plugin my_awesome_plugin

Step 2: Write Your Package Code

Open the file lib/my_awesome_package.dart.
Let’s write a simple utility function that capitalizes the first letter of any string:

library my_awesome_package;

String capitalize(String text) {
  if (text.isEmpty) return text;
  return text[0].toUpperCase() + text.substring(1);
Enter fullscreen mode Exit fullscreen mode

That’s it, you’ve written your first reusable function inside a Flutter package!

Step 3: Add Tests

Testing ensures that your package works correctly and builds trust among users.

Go to the test folder and add a new file named my_awesome_package_test.dart:

import 'package:flutter_test/flutter_test.dart';
import 'package:my_awesome_package/my_awesome_package.dart';

void main() {
  test('capitalize function works', () {
    expect(capitalize('flutter'), 'Flutter');
  });
}
Enter fullscreen mode Exit fullscreen mode

Then run your tests:
flutter test

Step 4: Update pubspec.yaml

Before publishing, fill in your pubspec.yaml with proper details:

name: my_awesome_package
description: A simple Flutter package that capitalizes text.
version: 1.0.0
homepage: https://github.com/yourusername/my_awesome_package
repository: https://github.com/yourusername/my_awesome_package
issue_tracker: https://github.com/yourusername/my_awesome_package/issues
Enter fullscreen mode Exit fullscreen mode

Don’t forget to add a LICENSE file (e.g., MIT License) and a README.md explaining how to use your package.

Step 5: Test It Locally

Before going public, you should test your package in another project.

In another Flutter app’s pubspec.yaml, add your package as a dependency:

dependencies:
  my_awesome_package:
    path: ../my_awesome_package
Enter fullscreen mode Exit fullscreen mode

Then run:

flutter pub get

Now try importing and using your package in your Flutter app:

import 'package:my_awesome_package/my_awesome_package.dart';

print(capitalize("flutter"));
Enter fullscreen mode Exit fullscreen mode

If it works, you’re ready to go live!

Step 6: Publish to pub.dev

1- Sign in to pub.dev

Go to pub.dev
and sign in with your Google account.

2- Check before publishing

Run this command in your package directory:

flutter pub publish --dry-run

This checks for potential issues (like missing fields or files).

3- Publish it!

If everything looks good, run:

flutter pub publish

Confirm with y when prompted.

Congratulations!
Your Flutter package is now live on pub.dev for the world to use!

Step 7: Maintain and Improve Your Package

  • Once your package is live:
  • Follow semantic versioning (1.0.1, 1.1.0, etc.)
  • Keep your README updated
  • Add CHANGELOG.md to track updates
  • Encourage contributions from others
  • Fix issues and respond to feedback

The better maintained your package is, the more developers will trust and use it.

Wrap-Up
Creating and publishing a Flutter package might seem complicated, but it’s actually straightforward once you’ve done it once.
It’s also one of the best ways to contribute to the open-source community and grow as a Flutter developer.
If you found this article helpful, don’t forget to leave a ❤️, comment, or follow me for more Flutter tutorials and open-source tips!

Top comments (0)