Creating your first Flutter package

Carlos Morales Pereira
Barkibu Development
3 min readNov 13, 2019

--

Before creating your package, you should first take a look at pub.dev and check if there is already a published solution for your problem. There, you will find lots of Dart and Flutter libraries created by the thriving community. Using those libraries in your application is as easy as adding a line to your pubspec.yaml file. If you come from other languages, this should remind you of npm, composer, etc.

If you can’t find there any package that suits your needs, you can easily create your own. Keep on reading :)

The first thing you need to know is that Flutter has 2 kinds of packages: general (or Dart) packages and plugins. The main difference between them is that plugins contain platform-specific code for Android and/or iOS and Dart packages don’t.

Using the flutter create command we can create both Dart packages and plugins, depending on the flag we set.

Note: package names must be lowercase with underscores between words.

Creating a Dart (or general) package:

flutter create --template=package our_package_name

This creates a bunch of files and folders under the our_package_name folder:

  • .idea/: to provide our editor (Android Studio is the recommended one) with some help.
  • lib/: Where our main code should reside.
  • test/: for our tests (what a surprise, huh?)
  • .gitignore, README.mdand some metadata files among which the most important is pubspec.yaml(and the accompanying pubspec.lock). As stated earlier, in pubspec.yaml we set up our dependencies, package info, version, etc.

The command line tool also created a couple of scaffold files in lib/ and test/:

  • lib/our_package_name.dart:the entry point to our library, it must have the same name as the package. By default, a sample Calculator class is created.
  • test/our_package_name_test.dart:with a sample test for the calculator class

Now, all we need to do is develop our awesome package, keeping tests updated. Simple, right?

Creating a Plugin package

What if our package needs to use a platform-specific API (Android and/or iOS)? In that case, we would create a plugin:

flutter create --template=plugin our_package_name --org=com.our_org_name

Note: By default, the plugin project uses Swift for iOS code and Kotlin for Android code. If you prefer Objective-C or Java, you can specify the language using -i for iOS and/or -a for Android.

Flutter creates the same folders as in a Dart package but, in addition, it also creates:

  • android/
  • ios/
  • example/: this folder contains a sample app that uses our plugin, so we can check with a real device how our work develops.

Sample application for your package

As you can see from above, plugin template creates an example app to showcase how your plugin works, but package template doesn’t.

If we want to include that example app within our package, we have to create it manually.

In our package root, we launch this command:

flutter create example

Then, we need to edit pubspec.yamlto include our package as a dependency:

dev_dependencies:
our_package_name:
path: ../

All that remains is to go inside example/lib, import our package and create some sample views.

Do you like Flutter? Join us!

This series of posts is part of Barkibu’s onboarding guide. We are the largest online pet care community in the world and we work with the latest technologies in a welcoming and pet-friendly office. Sounds interesting? We are hiring!

--

--