Creating and publishing a Flutter package

Akshat Sharma
Flutter Clan
Published in
4 min readJun 30, 2021

If you are having a problem and you are able to solve it, it's highly likely that someone else also might have faced it or will face it in the future. In the programming universe, a problem can be anything whose solution is not straightforward. If a developer is able to figure out a way to solve the problem in a better manner, it should be shared with the community as well. Flutter gives us a very simple tool to share our solutions globally. In this article, we’ll be exploring pub.dev and its wide use cases.

All Flutter developers make use of packages available at pub.dev in their applications. A package can be of two types, a Dart package or a Plugin package. A Dart package is a package that solves a problem in Dart language. It does not make use of any platform-specific APIs. Whereas a Plugin package is a package that uses both Dart and platform-specific APIs to make things work. In a Plugin package, Dart code communicates with native code written either in Objective C, Java, Swift, or Kotlin.

For example, we had to fetch the current badge number on the App Icon in iOS. We learned the process to get it, in this article. So, if anyone else faces this use case, he/she can follow the steps and get it done. Or one can just import a package and straight away get the count using Dart. The latter sounds easy and less time-consuming. So let’s get into developing a package.

flutter create --org com.aks --template=plugin --platforms=ios -i swift app_icon_badge

Execute the above command in the terminal. Let’s break it down. We have given organization only to be used for a sample app that will be using the package. template specified can be either plugin or package. Platform if specified will tell which platform this plugin will support. Available platforms are: android, ios, web, linux, macos, and windows. For our use case, we will only make a plugin for ios. As you all know iOS development is done using swift or Objective C, we get a choice to use any one of these. Our Dart code will be communicating with either swift or Objective C whichever language we give after -i. The last parameter is the name of the package.

After executing the following command, Flutter creates all the necessary files and gives us all the placeholders we require. The ease of development with Flutter is mind-blowing. Openlib/app_icon_badge.dart. You’ll see a getter for fetching platformVersion. Let’s create our own getter function.

static Future<int> get count async {
final int badgeCount = await
_channel.invokeMethod('getBadgeCount');
return badgeCount;
}

Well, that’s it from the Dart side.

Now open ios/Classes/SwiftAppIconBadgePlugin.swift file. Please note, the name will differ as per your package name. In this file, you’ll find a function handle that gets invoked whenever we send a message from the Dart side. In this function, you can directly return the count like the following.

public func handle(_ call: FlutterMethodCall, result: @escaping
FlutterResult) {
result(UIApplication.shared.applicationIconBadgeNumber)}

But this will return the same result even if we invoke a method with any name. Therefore as a best practice, we need to have a check for the method name.

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getBadgeCount":
result(UIApplication.shared.applicationIconBadgeNumber)
default:
result(FlutterMethodNotImplemented)
}
}

This is it, now to fetch the current badge count all we need to do is this.

final int result = await AppIconBadge.count;

We are done creating a package, now let’s focus on getting it published.

First of all, push your code to Github in a public repository. As you are contributing this code to the community, this will be open source. One more benefit you can get out of this is that users might start contributing to your repository to serve their own purpose.

Open pub.dev and sign in using your Google account. This step is very crucial because while publishing the package, Flutter publisher will ask for authentication.

Make sure to add the description and usage inREADME.md file which will explain the functioning of the package on the face itself.

In pubspec.yaml file add the link of the repository for the key homepage. You can remove the authorkey as it is deprecated now.

Make sure to add LICENSE. A very easy solution to get the contents of LICENSE is from GitHub itself while creating a new repository.

In CHANGELOG.md file write down the current changes. Adding the date to the list also helps to understand the evolution of the package. For example.

## [0.0.1] - 24th June 2021

* Added support for getting the current app icon badge count.

That’s all we need to do.

Before publishing run the following command from the root of the project. It will help you figure out any issues remaining for successfully publishing your package.

flutter pub publish --dry-run

Finally, after resolving all issues you can run the final command.

flutter pub publish

After successful authentication, your package will be live for usage.

Until next time!!

--

--