Sub topic 17
Preparing and publishing to pub.dev
This chapter takes the battery_info plugin from a working repository to a published package on pub.dev. It assumes you can already run the plugin from its example app on Android and iOS, and that its Dart tests pass. It also assumes you are comfortable with pubspec.yaml, Gradle files, and a CocoaPods podspec.
What publishing involves
Publishing uploads an archive of your package to pub.dev. The archive is permanent. You can retract a version within seven days, but you cannot delete it. Other developers may already depend on it by then, so treat publishing as a release step and not as a backup.
The process has five steps.
Complete the pubspec metadata
The generated pubspec.yaml has placeholder text. pub.dev reads it to build your package page, and the publish command rejects some placeholders outright.
name: battery_info
description: Reads the battery level on Android and iOS through a MethodChannel, with typed errors and a change stream.
version: 1.0.0
repository: https://github.com/example/battery_info
issue_tracker: https://github.com/example/battery_info/issues
topics:
- battery
- device
- plugin
environment:
sdk: ^3.0.0
flutter: ">=3.10.0"
dependencies:
flutter:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.battery_info
pluginClass: BatteryInfoPlugin
ios:
pluginClass: BatteryInfoPluginCheck these points:
descriptionshould be between 60 and 180 characters. A short description costs you pub points, and a very long one is a validation warning.versionfollows semantic versioning. Use0.0.1or0.1.0for early releases. Start at1.0.0only when you accept that breaking changes need a major bump.repositoryandissue_trackergive users a place to report problems. Userepositoryand not the olderhomepagefield unless you have a separate site.topicsaccepts up to five lowercase tags that help search.publish_to: noneis added to app projects. A plugin template does not include it, but remove it if you copied one in.- The
platformsmap underflutter.pluginis what makes pub.dev show "Android" and "iOS" platform badges. If you list only the platforms you implemented, the badges stay honest.
Check the native files
The Dart linter does not look at your Gradle or CocoaPods files, so check them yourself. Plugin authors most often forget these two.
The podspec in ios/ comes with template placeholders such as http://example.com. CocoaPods validates the file when an app builds, and a bad podspec breaks every consumer.
Pod::Spec.new do |s|
s.name = 'battery_info'
s.version = '1.0.0'
s.summary = 'Reads the battery level on iOS.'
s.description = <<-DESC
Reads the battery level on iOS for the battery_info Flutter plugin.
DESC
s.homepage = 'https://github.com/example/battery_info'
s.license = { :type => 'BSD-3-Clause', :file => '../LICENSE' }
s.author = { 'Example Dev' => '[email protected]' }
s.source = { :path => '.' }
s.source_files = 'Classes/**/*'
s.dependency 'Flutter'
s.platform = :ios, '12.0'
s.swift_version = '5.0'
endKeep s.version equal to the pubspec version. Run pod lib lint battery_info.podspec from the ios/ directory to catch errors.
On Android, open android/build.gradle and confirm three things:
namespacematches thepackagevalue in your pubspec.minSdkis the lowest API level you have tested.- You have no leftover test-only dependencies or local file paths.
Write the documents users see
pub.dev renders four files on your package page.
| File | What pub.dev does with it |
|---|---|
| `README.md` | Shows it as the package's main page. Include install steps, a short usage example, and any permissions or platform setup. |
| `CHANGELOG.md` | Shows it on the Changelog tab. Use one `## version` heading per release. |
| `LICENSE` | Required. Publishing fails validation without one. An OSI-approved license such as BSD-3-Clause or MIT earns pub points. |
| `example/` | Shows the example app's `lib/main.dart` on the Example tab. |
A first changelog entry looks like this:
## 1.0.0
* Adds `BatteryInfo.getBatteryLevel()` for Android and iOS.
* Adds `BatteryInfoException` for native errors.
* Adds `onBatteryLevelChanged` stream.Platform setup belongs in the README. If a method needs a permission, an Info.plist key, or a manifest entry, say so there, because users will not read your native source.
Document the public API
Pana, the analyzer pub.dev runs on your package, checks that public members have documentation comments. Use /// comments and explain the failure behavior, not only the success case.
class BatteryInfo {
static const MethodChannel _channel = MethodChannel('battery_info');
/// Returns the battery level as a percentage from 0 to 100.
///
/// Throws a [BatteryInfoException] with code `UNAVAILABLE` if the
/// platform cannot report the level, for example on a simulator.
Future<int> getBatteryLevel() async {
try {
final int? level = await _channel.invokeMethod<int>('getBatteryLevel');
return level!;
} on PlatformException catch (e) {
throw BatteryInfoException(e.code, e.message);
}
}
}Run dart doc locally to see the generated pages. Make sure lib/battery_info.dart exports everything users need, and keep implementation files under lib/src/.
Validate before you publish
Run the checks in this order:
flutter analyze
flutter test
dart pub publish --dry-runThe dry run builds the archive and runs the same validators as the real publish, without uploading anything. It reports:
- Errors, which block publishing. Missing
LICENSE, a missingdescription, andpublish_to: noneare typical. - Warnings, which ask for confirmation. Uncommitted git changes and a missing
CHANGELOG.mdentry are typical. - The list of files in the archive. Read it. If you see a build output folder, local
.envfiles, or keystores, stop.
To exclude files, add a .pubignore file. It uses .gitignore syntax and replaces .gitignore when both exist, so repeat any .gitignore rules you still want. The archive limit is 100 MB compressed, so keep generated binaries and large assets out of it.
Publish
- Sign in with a Google account. The first publish opens a browser for authorization.
- Run
dart pub publish. The tool prints the file list and asks foryto confirm. - The account that publishes becomes an uploader for the package. For a team, create a verified publisher on pub.dev first and transfer the package to it, so ownership does not depend on one person.
For repeated releases, pub.dev supports automated publishing from GitHub Actions. You enable it in the package's Admin tab, name the repository, and set a tag pattern such as v{{version}}. Pushing a matching tag then publishes without a stored secret, because GitHub signs a short-lived OIDC token for each run.
After publishing
Open your package page and check the score tab. Pana awards up to 160 pub points across five areas: Dart file conventions, documentation, platform support, static analysis, and up-to-date dependencies. Fix what it flags and release a patch.
Use these rules for later releases:
- Bump the major version for breaking changes to the Dart API. Changing a channel name or method string is breaking only if users call it directly, which your Dart wrapper should prevent.
- Bump the minor version for new methods or streams, and the patch version for fixes.
- Raising
minSdkor the minimum iOS version is a breaking change for consumers. Treat it like one. - Retract a version within seven days if it is badly broken, then publish a fixed one. Use
discontinuedonly when you stop maintaining the package.
Exercise
You run dart pub publish --dry-run and it reports no errors, but the file list includes example/ios/Runner/GoogleService-Info.plist. What should you do before publishing, and why?
Show answer
Stop and exclude the file. The dry run only validates package structure and does not detect secrets. Add the path to a .pubignore file (repeating any .gitignore rules you still need) and run the dry run again to confirm the file is gone. Better still, remove the file from the example app, because published versions are permanent and anyone can download the archive.
Exercise
Your plugin's pubspec says version: 1.2.0, but ios/battery_info.podspec still says s.version = '0.0.1'. Name one consequence and the fix.
Show answer
The iOS pod reports a different version from the package, which confuses debugging and release tracking. The fix is to set s.version to 1.2.0 and validate it with pod lib lint. Keep the two values in sync on every release.