[Flutter] Publish package to pub.dev

[Flutter] Publish package to pub.dev

2023-06-20 hit count image

Let's see how to create and publish it to pub.dev on Flutter.

Outline

When you develop the app with Flutter, sometimes you need to make the package for common libraries or opensource and publish it to https://pub.dev/.

In this blog post, I will introduce how to publish the package for the Flutter app to pub.dev.

Prepare publishing pub.dev

To publish the package to pub.dev, you need to signup pub.dev and register Publisher.

First, go to the https://pub.dev/ site. And signup and signin it.

Publish pub package - Login pub.dev site

And then, click the My pub.dev > Create publisher menu on top right of the page.

Publish pub package - Create publisher

To publish the package to pub.dev, you need a domain that is registered Google Search Consol. If you have the domain, insert the doamin to Domain Name, and click the START VERIFICATION button.

Publish pub package - Add domain name

And then, the process of verifying the domain will proceed. After passing all processing, you can see the Publisher page like the following.

Publish pub package - Publisher page

Create package project

Next, you need to create a package project to publish it ot pub.dev. Execute the following command to create the package project.

# flutter create --template=package [package_name]
flutter create --template=package deku_publish_example

And then, modify the lib/[package_name].dart file to make the contents of the package. In this blog post, I just create the lib/round_button.dart file and modify it like the following.

import 'package:flutter/material.dart';

class RoundButton extends StatelessWidget {
  final String label;
  final Color? backgroundColor;
  final VoidCallback? onPressed;
  final bool? isLoading;

  const RoundButton({
    required this.label,
    this.onPressed,
    this.backgroundColor,
    this.isLoading,
    super.key,
  });

  Widget _renderLabel() {
    if (isLoading == true) {
      return const CircularProgressIndicator(
        color: Colors.white,
        strokeWidth: 3,
      );
    }

    return Text(label);
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        onPressed: isLoading == true ? null : onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: backgroundColor,
          padding: const EdgeInsets.symmetric(vertical: 12.0),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(24.0),
          ),
        ),
        child: _renderLabel(),
      ),
    );
  }
}

And the, modify the lib/deku_publish_example.dart file like the following.

library deku_publish_example;

export 'round_button.dart';

Of course, You can modify the test/deku_publish_example_test.dart file to make the test code to check the feature is working well.

Create example

Now, you need to create an example that uses the package. Execute the following command to create the example project.

# cd deku_publish_example
flutter create example

And then, open the example/pubspec.yaml file and modify it like the following to use the package we made above.

...
dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  deku_publish_example:
    path: ../
...

To use the package installed like this, open the lib/main.dart file and modify it like the following.

import 'package:deku_publish_example/deku_publish_example.dart';
...
          children: <Widget>[
            ...
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            RoundButton(
              onPressed: _incrementCounter,
              label: 'Increment',
            ),
          ],
...

After modifying the example to use the package, when you run it on the simulator, you can see the package is working well.

Publish pub package - Package example

Modify pubspec.yaml file

To publish the package, you need to add and modify the following contents of the pubspec.yaml file.

name: [Package Name]
description: [Description]
version: 0.0.1
homepage: [Homepage URL]
repository: [Repository URL]

Changelog file

You need to create the Changelog file for keeping the changes of the version. Create the CHANGELOG.md file and modify it like the following.

# CHANGELOG

## 0.0.1

License file

You need to write the License of the package. Open the LICENSE file and modify it like the following.

MIT License

Copyright (c) [Project Owner]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

The content is MIT license provided by default in GitHub.

Readme file

Also you need to modify the README.md file. In this blog post, I just modify it like the following.

# Deku publish example

This is an example project for deployment.

Publishing test

The package is ready to publish. Let’s test publishing by running the following command.

flutter packages pub publish --dry-run

If there is no issue, you can see the following result.

...
Package has 0 warnings.
The server may enforce additional checks.

Publish

Now, execute the following command to publish the packge to pub.dev.

flutter packages pub publish

And then, you can see the following screen about asking the publishing or not.

...
Publishing is forever; packages cannot be unpublished.
Policy details are available at https://pub.dev/policy

Do you want to publish deku_publish_example 0.0.1 to https://pub.dev (y/N)?

Press the y key to run publishing. And then, you can see the following result.

...
Uploading...
Successfully uploaded https://pub.dev/packages/deku_publish_example version 0.0.1.

Navigate to https://pub.dev/packages/deku_publish_example shown in the result, and you’ll see that the package is published well like the following.

Publish pub package - Package is published

To publish without problems, the version of CHANGELOG.md file and pubspec.yaml must be the same, and the version must not have been published to pub.dev.

Automate publishing

After publishing the package once, you can automate publishing by useing GitHub Actions. About automating the publishing, please see the following link.

Compleated

Done! We’ve seen how to create a package to use on Flutter and publish it to pub.dev. I hope you also refer to this blog post and create and publish a common widget library or open source to pub.dev.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts