Saturday, October 27, 2018

Flutter - Publish plugins and packages

publish-flutter-plugin
As a mobile developer, we always need some already developed own or third-party libraries. The library makes our project development fast,  save development time and some time provide us a great performance. The project development may become hard If we do not use all those third-party libraries in our projects.


In this post, you will learn how can create and publish your own Flutter plugin and package, which people can effortlessly add and use in their projects.

I'll provide a walk-through for creating and publishing a package on Dart Pub. After that, you'll able to share something you’ve made like a new kind of widget or a tool that might be helpful to other developers.

Packages & Plugin in Flutter
In Flutter, you can create two kinds of libs for an application.

Package: It is maybe a new widget or a set of Dart classes.

Plugins: In this package, we can write native code and 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



Let's start it with the following steps.

Creating a Package
1. Creating a package with Android Studio is remarkably easy. When creating a Flutter project in Android Studio, this is the first window that pops up:



select Flutter Package tab and in the next window give a name to package and finish it

  2. A package is structured exactly like a Flutter application. The lib directory still holds our main Dart code and the pubspec.yaml still holds our dependencies. We'll keep all files in libs directory. 


once you created your package and test it on a real device. Now, before making it publish. There are a few things that we should take care.


3. Keys for publish:
  • All code that we want to share should be in the lib directory.
  • The pubspec.yaml should contain the details about our package version number, author, email and homepage where the package is hosted. like:
  • The LICENSE file should contain a license for the use of the package, usually given a BSD-style license.
  • The README should neatly document the use of the package and any important things to note.
  • Adding an example directory is helpful for developers using the package and also adds an Example tab when the package is published.
  • The CHANGELOG logs the changes in every version of the package.


Publishing the package

4. Before publishing the package, make sure to run pub publish — dry-run which essentially simulates uploading the package without actually publishing it. It will report any missing files or attributes will be pointed out.



5. Once everything correct reported by above command. Now run flutter packages pub publish to publish it. You will see an auth URL path visible:


you can copy and click on that URL to provide auth. Once google account login window comes. Now login with the same email that you have provided in the pubspec.yaml of the package. After succesful logged in google count. You will see that the following message.



you will get the same message from Flutter pub:


now, wait for 5 to 10 minute. You will see it's published on Dart Pub.


you can see all detail, when click on the published package.



Writing a package is not different from creating a normal app. So, if you have any widget and native plugin. You can share with the Flutter community. As we have publish a widget image_picker_ui: ^0.0.1 that will provide option to pick image from camera and device gallery.

I hope, you got the basic idea for a publish a package and plugin in the  Flutter Pub. If you have any doubt and query, please feel free to ask it from the comment section below and you can ask it on SLACK.

Share:

Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...