Flutter Packages — Part 1

Amrit Pandey
3 min readMar 31, 2024

Writing modular code is key to good code that can be scaled in future and can be reused by different developers. Packages and plugins in Flutter will help us to achieve modularity in our code.

Choose the right option for the requirement

There are mainly 3 kinds of packages in Flutter.

  1. Dart Package
  2. Plugin Package
  3. FFI plugin package

Dart Package is a package written in Dart with the help of a flutter framework. The package can not contain any platform-specific (iOS, Android etc) code. flutter create --template=package package_name . eg creating reusable widgets which can be shared across multiple projects.

Plugin Package can contain Dart code along with platform-specific (iOS, Android etc) code. A plugin package can be written for any platform or combination of platforms. flutter create --template=plugin plugin_name . eg Using native libraries to support implementation in native platform. Scanning QRcode in iOS via VisionKit and in Android via CAMView.

They can be further classified into

  1. Federated plugins: For providing support to different platforms, they can be split into separate packages. It can use one package for iOS, another one for Android, another for web etc. With this approach, native developers can develop functionality by writing code in their platform.
  2. Endorsed federate plugin: What if web dev finds a Flutter plugin, that’s not supported on the web? Web dev can write web-specific code, and the original author of the plugin can add this web_specific implementation as a dependency in pubspec.yml file. eg Flutter has a package image_picker but is available only in iOS and Android, now a web dev wants to add web implementation to this package, so he has written image_picker_web . The original author image_picker can add image_picker_web to pubspec.yml file. Any app using image_picker a plugin as a dependency will find web support for this package.
  1. Non-endorsed federated plugin: In the earlier example, if image_picker_web is not added to image_pickerby the original author, it is called a non-endorsed federate plugin. App users need to include both image_picker_web and image_picker as a dependency.

FFI Plugin package is FFI (foreign function interface) that allows Flutter apps to make use of existing native libraries that expose a C API, flutter create --template=plugin_ffi ffi_name . This can expose any Java or Kotlin, JavaScript, Obj-C or Swift library to flutter, eg AVAudio library in iOS can be exposed to flutter code. I am going to cover this as a separate topic.

This will provide you with an overview of plugins and packages to write modular code in flutter.

In the next topic, I will discuss more about Plugin Packages.

Stay tuned for Flutter Packages — Part 2. Happy coding.

--

--