Sub topic 1
What a plugin is and when you need one
Prerequisites
This chapter assumes you can write Dart and build Flutter apps with widgets, pubspec.yaml and flutter pub add. You should be able to read short Kotlin and Swift snippets. You don't need to write native code yet. Later chapters set up Android Studio and Xcode and build a working plugin. Here we only decide what kind of package you need.
The problem a plugin solves
Flutter draws its own user interface and runs your Dart code in its own engine. Dart code cannot call Android or iOS system services directly. Examples are the battery level, the camera, Bluetooth and the keychain. Those services are exposed through Kotlin, Java, Swift and Objective-C APIs.
A Flutter plugin is a package that contains Dart code plus platform-specific native code, and that connects the two. An app developer adds the plugin as a normal dependency and calls plain Dart methods. The plugin forwards each call to the native side and returns the result.
Through this tutorial you will build battery_info. It reads the battery level on Android and iOS, and it is small enough to follow in full.
Three kinds of package
Flutter has three package types that are often confused. The right choice depends on where your code has to run.
| Package type | Contains | How Dart reaches native code | Typical use |
|---|---|---|---|
| Dart package | Dart only | Not applicable | Parsing, state management, widgets, API clients |
| Plugin package | Dart plus Kotlin/Java and Swift/Objective-C | Platform channels | Using platform APIs such as battery, sensors, system pickers |
| FFI plugin | Dart plus C, C++ or Rust compiled for each platform | `dart:ffi` | Wrapping an existing native library, performance-critical code |
Dart packages
A Dart package has no native code. It runs the same everywhere Dart runs. If you can solve the problem with Dart and Flutter's own libraries, write a Dart package. It is the cheapest to build and maintain, and it works on every platform Flutter supports, including the web and desktop.
Plugin packages
A plugin package declares native implementations in pubspec.yaml and ships them inside the package. This is the type you need when the capability exists only as an Android or iOS API. This tutorial focuses on plugins.
FFI plugins
An FFI plugin uses dart:ffi, Dart's foreign function interface, to call C-compatible functions directly. There is no message passing and no asynchronous hop through the engine's messenger. You would choose it when you already have a C or C++ library, or when per-call overhead matters, for example in image processing or cryptography.
FFI is not a way to call Android's Java APIs or iOS's Swift APIs. Those are not C functions. Calling BatteryManager or UIDevice needs a platform channel, so battery_info is a plugin and not an FFI package.
A decision procedure
Work through these questions in order.
- Can the feature be written in Dart with existing packages? If yes, write a Dart package.
- Does it need an Android or iOS system API, such as a permission-gated service or a vendor SDK? If yes, write a plugin.
- Does it wrap a C or C++ library, or need very fast synchronous calls? If yes, consider an FFI plugin.
- Does an existing plugin on pub.dev already do it? If yes, use it or contribute to it before writing your own.
How a plugin works at a glance
A plugin has three layers.
- The Dart API. This is what app developers import. It hides channel names and message formats.
- The channel. A platform channel sends named messages between Dart and native code. The
MethodChannelclass handles request and response calls. - The native implementations. A Kotlin class for Android and a Swift class for iOS. Each one receives calls, runs platform code and returns a result or an error.
Each call is asynchronous. The Dart side returns a Future, and the native side answers later. Values are encoded with a standard codec that supports a fixed set of types such as numbers, strings, lists and maps. Chapter 4 covers this in detail.
Why plugins have a cost
A plugin is more work than a Dart package. Keep these costs in mind before you start.
- Two or more codebases. You maintain Dart, Kotlin and Swift. A bug may live in any of them.
- Behaviour differences. Android and iOS rarely match exactly. In
battery_info, Android returns a level of-1throughgetIntPropertyin some cases, and iOS reports a negative level when monitoring is off or on a simulator. Your Dart API must give callers one consistent contract. - Tooling requirements. Contributors need Android Studio and Xcode, and CI must build both.
- Version drift. New Android and iOS releases deprecate APIs. Plugins need regular updates.
- Slower tests. Dart code is tested with fast unit tests, but native code needs device or emulator tests.
You can reduce these costs by keeping the native code thin. Put logic in Dart wherever you can, and let native code only translate between Dart and the platform API.
Signs you do not need a new plugin
Check these cases before you create a project.
- An existing, maintained plugin covers the capability.
battery_plusalready reads the battery level, sobattery_infoexists here only for learning. - The feature is available through Flutter's own services, such as
ClipboardorHapticFeedbackinpackage:flutter/services.dart. - The code is needed in one app only. You can write a platform channel inside the app's own
androidandiosfolders, with no separate package. Extract a plugin later if a second app needs it.
What you will build
By the end of this tutorial, battery_info will have a Dart API, Android and iOS implementations, error handling, tests, an example app, and a validated package ready for pub.dev. The next chapter generates the project with the flutter create --template=plugin command and explains the layout of the files it produces.
Exercise
You need to read the current Wi-Fi network name on Android and iOS. No maintained package does what you need, and the only access is through Android's WifiManager and iOS's NEHotspotNetwork. Which package type do you choose, and why is an FFI plugin the wrong choice?
Show answer
Choose a plugin package. The capability exists only as Kotlin/Java and Swift/Objective-C platform APIs, so Dart must reach it through platform channels. An FFI plugin calls C-compatible functions through dart:ffi, and WifiManager and NEHotspotNetwork are not C functions, so FFI cannot call them directly.
Exercise
Your team wants a function that validates and formats phone numbers using only string rules. Should it be a plugin? Name one cost that you avoid by choosing a different package type.
Show answer
No. The logic runs in Dart, so a Dart package is enough. It avoids costs such as maintaining Kotlin and Swift code, needing Android Studio and Xcode in CI, and testing native code on devices. It also works on web and desktop with no extra work.