Flutter Packages and Plugins: A Quick Primer

We've implemented playback of offline videos into a Flutter app. Here is what we learned.

Flutter Packages and Plugins: A Quick Primer

Last year I wanted to implement a new feature in one of our flutter apps, which was the ability to watch videos offline (which was surprisingly hard to do).

Once I found out, that the official video_player plugin does not support offline playback of locally stored m3u8 playlists, I excitedly went down the rabbit hole.

So here’s a quick summary of what I learned.

Package/Plugin Landscape

Packages

In the Flutter world we usually refer to packages as dart only units of code that can be imported as so called packages in the pubspec.yaml file.

Plugins

A plugin on the other hand, usually consists of dart code and native code. It’s used to expose native code to your dart UI code, famous examples are the camera plugin or in our case the video_player plugin.

However here is where the fun begins.

When you decide to write your own plugin, you ultimately have to understand that there are quite some different approaches you can take to do that.

If you want to use code of a native library that’s written in C, like sqlite for example, you can use dart:ffi https://dart.dev/interop/c-interop

There is also experimental support for objective-c and swift interop through package:ffigen https://dart.dev/interop/objective-c-interop

You can utilize package:jnigen to interop with Java and Kotlin, https://dart.dev/interop/java-interop.

To make the list complete there is dart:js_interop to work with javascript and browser API’s in case you do web development, https://dart.dev/interop/js-interop.

Note that ffigen and jnigen are considered experimental as of this writing.
So what’s the current approach then?

Currently so called PlatformChannels are used to communicate with native SDKs, wich requires sending/receiving messages on both sides (the dart and the native side of the plugin).

When we speak flutter plugins, there are even more subtypes like:

  • Federated plugins (Plugins that have one or more platform plugins)
  • Endorsed federated plugins (Plugins that already import platform plugins)
  • Non-endorsed federated plugins (Plugins that do not import platform plugins, so you have to do it)

I don’t want to describe all that in detail, as it is already perfectly available in the official documentation here: https://docs.flutter.dev/packages-and-plugins/developing-packages

The only thing that’s important:
If you write a plugin, make it be a federated plugin — it’s the “official” way.

As I try to do more regular, but short and concise stories, I’ll stop right here.
In the next story we’ll talk about writing or modifying an Endorsed federated plugin that’s using PlatformChannels like video_player (https://pub.dev/packages/video_player).


Thanks for reading!
If you liked this post, please subscribe to receive more useful content.
Go to https://seadev-studios.com for more infos about our company.