Developing a Flutter Plugin: Playify

I. Berat Kaya
4 min readAug 25, 2020

The development of plugins in open source communities is very important. In this post I will explain my process of developing a Flutter Plugin. Playify is a Flutter plugin for playing music and fetching music metadata. Rather than making this a “How to develop a Flutter plugin” tutorial, I will try to explain my mindset, difficulties, and the simplicity of developing a Flutter plugin.

Stating The Problem

I decided to develop Playify because of my desire to develop a music player, and not being able to find any plugins that would let me interact with iOS’s Media Player Library. Having no experience in writing native code, I decided to take up this challenge to learn more, and help the open source community of Flutter.

Creating The Plugin

A Flutter Plugin can be easily created with running flutter create --template=package pluginName from the terminal. It will create all the files necessary to develop and publish the plugin.

Learning Swift

I learned just enough Swift to develop this plugin. It took me about 2–3 days to learn. It is a fairly easy language with a very modern syntax, so it is easy to learn. I would recommend everyone to take a look at it. It has some unique concepts.

Learning Kotlin

I learned a small amount of Kotlin for this project. This was definitely the hardest part of the project. I found out I preferred writing Swift instead of Kotlin. After some attempts I decided that since many well developed plugins already exist for Android (such as audioplayers), I decided that I will integrate one of these plugins into Playify.

Writing The Plugin

Writing the plugin was more easy than I thought it would be. I was quite impressed with the simplicity of how Flutter handles the channel between Swift and Dart. The communication felt similar to sending an API request to a server from a client and getting the result. The Media Player Library was developed quite well by Apple and it was very simple to use with the exception of minor bugs that exist in iOS 13.

//An example function in Swift where the
//playback time of a song is set
//Check the called method's name
if
(call.method == "setPlaybackTime") {
//Check if the arguments of the call from Flutter exist
//In this case there is only one argument named "time"
guard let args = call.arguments as? [String: Any] else {
result(Bool(false))
return
}
//Get the "time" argument
let
time = (args["time"] as! NSNumber).floatValue
//Set the playback time via the Media Player Library
self
.setPlaybackTime(time: time)
//Return that operation was successful
result(Bool(true))
}

The call object is a FlutterMethodCall that contains information about the type of the call and the given parameters to Swift. result() is the result that will be sent back to Dart once the execution of the function has ended. Flutter communicates with Swift through the FlutterMethodCall and result() function.

Now we can invoke our Swift function from Dart.

//Our Method Channel that will let Dart communicate with Swift
MethodChannel playerChannel = const MethodChannel('channelName');
//The Dart function that communicates with Swift
Future<bool> setPlaybackTime(double time) async {
bool result = await playerChannel.invokeMethod(
'setPlaybackTime', //The name of the Swift function
<String, dynamic>{"time": time} //The "time" argument
);
return result;
}

You can view more of these examples at Playify’s Github Page.

Testing The Plugin

You can test the plugin using the example project that comes with the creation of the project. symlinks are created so you can use your own plugin from the example project. You can run the project both from the terminal or Xcode. I preferred Xcode since I had to also log from Swift and I had issues with Dart’s logs when running the project from the terminal.

Publishing The Plugin

Publishing was the easiest part of the project. You need to set the version of the plugin in Pubspec.yaml and the repository link. Then run flutter pub publish --dry-run to check for any errors. If no errors exist, run flutter pub publish to publish the plugin on pub.dev and your plugin will appear soon after.

Conclusions

If I had known the simplicity of developing a plugin, I probably would have started developing plugins sooner, even without having any experience in Swift or Kotlin. Developing a plugin also helped me learn more about how Flutter works and made me feel more of a part of The Flutter Community. I would most definitely recommend to every developer to at least develop one plugin for the experience.

Playify App

Screenshot of Playify The App

I am also releasing Playify as an app currently only available on iOS on the App Store. Feel free to check out the code of the app.

The Playify App: Code

Flutter Logo

Make sure to checkout flutter.dev to learn more about Flutter.

Useful links:

--

--

I. Berat Kaya

I study computer engineering at Istanbul Technical University and I am in my senior year. I’m interested in Node.js, Flutter, React Native, and Rust.