My Hexaville
Spead the Word
flutter create toast plugin

How To Create A Flutter Toast Plugin

Sometimes you’d want to use native platform API’s but Flutter does not let you do it, or maybe you even want to use libraries from Android and iOS that achieve a similar goal. All of this is possible by creating Flutter plugins

Flutter plugins

As you probably realized already, a Flutter plugin is a way to call native Android/iOS API’s. In fact, there are already several official plugins out there

Things like creating custom push notifications with declaring actions, handling permissions are one of the many other features that would be possible to implement

Flutter plugins are pretty lightweight. You actually don’t even need to create a separate project, just declare an API method and provide native implementations

So as a simple example, I’m going to create a Flutter toast plugin that display Android toast messages and iOS alerts. There’re some iOS toast libraries, but alerts are the default way for displaying messages to user on iOS I think

And then I’m going to show how to distribute this plugin though GitHub so that you can use it for other projects

flutter create toast plugin

flutter create toast plugin

Creating a Plugin

In Android Studio, create a Flutter plugin through setup wizard and you’ll get all the basic stuff

flutter create toast plugin

Here’re the main elements in the generated project:

  • Dart API
  • iOS implementation
  • Android implementation
  • Flutter sample app

Flutter API

Let’s declare an API first

This is going to be a Dart file from root lib directory

Sample Code

Now let’s update the sample code to debug during development. This is my Dart file in example/lib directory

iOS implementation

I recommend editing Swift files in Xcode because it has a great support. What we need to edit is a Swift file from ios/Classes directory. You actually cannot open this directory in Xcode, but you can open example/ios first and then open that Swift file

We have two arguments, a FlutterMethodCall, from which we can get arguments and a FlutterResult, which can be used to return a result value through FlutterResult#result()

Android implementation

This code goes in android/src Kotlin file

Basically the same. The only thing here is that Android Studio does not give you autocomplete for Kotlin from the Flutter project directory, so you need to open up different android folders till you manage to enable that Kotlin autocomplete and be able to see Flutter SDK dependencies. Still haven’t figured out what exactly were the steps that solved the problem

Or you can just use Java and autocomplete will work just fine from your Flutter root directory

Distributing Through GitHub

A great thing about Dart is that you don’t need to deploy your libraries to the pub, to distribute it. You can just use a library from GitHub, just like with iOS libraries

And then call from any project you want


Alright, that’s all it is to create a Flutter plugin. You can get the source code here. For a next post, I’ll think of an interesting plugin idea. Stay tuned

About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!