Flutter Packages — Part 1
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.
- Dart Package
- Plugin Package
- 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
- 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.
- 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 packageimage_picker
but is available only in iOS and Android, now a web dev wants to add web implementation to this package, so he has writtenimage_picker_web
. The original authorimage_picker
can addimage_picker_web
topubspec.yml
file. Any app usingimage_picker
a plugin as a dependency will find web support for this package.
- Non-endorsed federated plugin: In the earlier example, if
image_picker_web
is not added toimage_picker
by the original author, it is called a non-endorsed federate plugin. App users need to include bothimage_picker_web
andimage_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.