Sub topic 17
Preparing and Publishing a Flutter Plugin to pub.dev
This chapter assumes that you can build Flutter apps, read Dart, Kotlin, and Swift, and use Android Studio, Xcode, Gradle, and CocoaPods. You should also have a working plugin with an example app and tests.
Publishing makes your plugin available through the Dart package repository, pub.dev. A published plugin needs more than working code. Users need clear documentation, predictable versioning, licenses, and a package that passes automated checks.
Prepare the package contents
Keep the package focused. The public Dart API belongs in lib/. Native Android and iOS code belongs in their platform directories. The example/ directory should contain a small application that demonstrates the main feature.
| File or directory | Purpose |
|---|---|
| `lib/` | Public Dart API |
| `android/` | Android plugin implementation |
| `ios/` | iOS plugin implementation |
| `example/` | Runnable demonstration app |
| `test/` | Dart and channel tests |
| `README.md` | Installation and usage instructions |
| `CHANGELOG.md` | User-visible release history |
| `LICENSE` | Legal terms for using the package |
| `pubspec.yaml` | Package metadata and dependencies |
Your README.md should show installation, a minimal usage example, supported platforms, required permissions, and known limitations. Explain any native setup that cannot be inferred from the plugin declaration. Keep examples synchronized with the current Dart API.
Choose a version and document changes
Flutter packages normally use semantic versioning. A version has the form major.minor.patch.
- Increase the patch number for backward-compatible bug fixes.
- Increase the minor number for backward-compatible features.
- Increase the major number for breaking API changes.
Record each release in CHANGELOG.md. Describe changes from the user's perspective. Do not publish a version that still says 0.0.1 unless that is intentional.
Update the metadata in pubspec.yaml before validation:
name: battery_info
description: Reads the battery level on Android and iOS.
version: 1.0.0
repository: https://github.com/example/battery_info
issue_tracker: https://github.com/example/battery_info/issues
topics:
- battery
- platform-integration
- flutter-pluginThe package name must be unique on pub.dev. The description should state what the plugin does, not how its internal channels work.
Validate before publishing
Run checks from the package root:
flutter pub get
flutter analyze
flutter test
dart pub publish --dry-runThe dry run lists files that would be uploaded and reports package warnings. Inspect that list for accidental files such as build output, private notes, credentials, or large archives. Fix warnings that affect documentation, licensing, platform support, or package quality.
When the package is ready, publish from the same package root:
dart pub publishThe command asks for confirmation. After publication, inspect the pub.dev page, verify the rendered README, and test installation from a separate sample project. Tag the corresponding commit in version control so the registry release and source history can be matched.
Exercise
Which command should you run to inspect the files and warnings before publishing?
Show answer
Run dart pub publish --dry-run from the plugin package root.