Publishing a Flutter Package on Dart Pub

Deven Joshi
CodeChai
Published in
4 min readAug 1, 2018

Recently, I published a story on Recreating the YouTube app in Flutter and I realised it’d be nice to have the same Flutter PIP widget up on Dart Pub. So I decided to create a Widget for the same.

I decided to write this article as a walk-through for creating and publishing a package and to encourage more people to share things they’ve created in Flutter.

What is a Package?

Packages allow code-sharing and reusability across Flutter/Dart apps. If you think something you’ve made like a new kind of Widget or a tool might be helpful to other developers, consider making a package out of it and uploading it to Dart Pub.

Dart Packages v/s Plugin Packages

In Flutter, there are two types of packages.

The first is a pure Dart package, like a new Widget or a set of Dart classes and is usually simply referred to as a ‘package’.

The second is kind of package makes it easier to access platform-specific functionality. For example, the “battery” package helps you check about the battery details. This requires you to access the platform, but the package does that for you. These kinds of packages are referred to as ‘plugins’.

Creating a Package

Creating a package with Android Studio is remarkably easy to do and kudos to the tooling team for Flutter.

When creating a Flutter project in Android Studio, this is the first window that pops up:

Instead of Flutter Application, choosing Flutter Package creates a basic package for you.

Taking a look at the project files

A package is structured exactly like a Flutter application is:

  1. The lib directory still holds our main Dart code.
  2. The pubspec.yaml still holds our dependencies.

The default package has a Calculator class and the ‘test’ directory has a simple test related to it. I’ll omit adding the code for brevity.

Here are a few of the key differences:

  1. All code we want to share should have a line declaring the library it belongs to.
library demo_package;

2. The pubspec.yaml should contain the details about our package version number, author, email and home page where the package is hosted

name: demo_package
description: A new Flutter package.
version: 0.0.1
author:
homepage:

3. The ‘LICENSE’ file should contain a license for the use of the package, usually given a BSD-style license.

4. The ‘README’ should neatly document the use of the package and any important things to note.

5. Adding an “example” directory is helpful for developers using the package and also adds an “Example” tab when the package is published.

6. The ‘CHANGELOG’ logs the changes in every version of the package.

Publishing the package

Publishing the package is as simple as running “pub publish” in the terminal.

Before publishing the package, make sure to run “pub publish — dry-run” which essentially simulates uploading the package without actually publishing it. Any missing files or attributes will be pointed out by the dry run.

For example if I don’t fill a few fields and do a dry run:

Some final thoughts

If you’re already proficient with normal Flutter application development, it never feels like we go out of our way to develop a package. Writing a package doesn’t feel very different from creating a normal app and that is really commendable. So if at any time you feel like something you make might be useful to the Flutter community, be sure to give packages a try.

This article was inspired by my experience in making a simple Picture-In-Picture package for Flutter.

Package Link

GitHub Link

The Flutter Pub is a medium publication to bring you the latest and amazing resources such as articles, videos, codes, podcasts etc. about this great technology to teach you how to build beautiful apps with it. You can find us on Facebook, Twitter, and Medium or learn more about us here. We’d love to connect! And if you are a writer interested in writing for us, then you can do so through these guidelines.

--

--

Deven Joshi
CodeChai

Google Developer Expert, Flutter | Technical Writer | Speaker