Sub topic 1
What a Plugin Is and When You Need One
A Flutter plugin is a Dart package that includes platform-specific code. The Dart API gives Flutter code a consistent interface, while native implementations call Android or iOS APIs. A plugin is useful when the operating system provides a capability that Dart and Flutter do not provide directly.
This chapter assumes you can write Dart and build Flutter applications. You should also have basic Kotlin or Java knowledge for Android and basic Swift knowledge for iOS. Android Studio, Xcode, and a working Flutter toolchain are useful for later chapters.
Why a Plugin Exists
Flutter code runs in Dart. Android APIs run on the Android runtime, and iOS APIs run on Apple frameworks. These environments do not share the same classes or operating-system services.
For example, a battery plugin might expose this Dart method:
final level = await BatteryInfo().getBatteryLevel();The Android implementation could read BatteryManager. The iOS implementation could read UIDevice.current.batteryLevel. The caller does not need to know which native API produced the value.
A plugin therefore has two responsibilities:
- Define a stable Dart API for Flutter applications.
- Translate each API operation into native Android and iOS operations.
A plugin is different from a normal Dart package and from an FFI package.
| Package type | Main code | Best for |
|---|---|---|
| Dart package | Dart only | Reusable logic that does not need platform APIs |
| Flutter plugin | Dart plus platform implementations | Android and iOS APIs, permissions, sensors, and native UI |
| FFI package | Dart plus C-compatible native library | Calling existing C or C-compatible code |
Choosing the Right Package Type
Use a plain Dart package when the feature can run entirely in Dart. Date formatting, validation, state management, and data models usually belong in a Dart package.
Use a Flutter plugin when you need a platform API or a native framework. Examples include Bluetooth, camera controls, background services, contacts, and battery information. Platform channels carry messages between Dart and native code.
Use FFI, or foreign function interface, when you need to call functions in a native library through a C-compatible interface. FFI is often a good fit for a portable C or Rust library. It is less convenient when the feature depends on Android components, iOS delegates, permissions, or lifecycle callbacks.
A plugin can also combine approaches. For example, its Android and iOS layers might use platform channels for operating-system integration and FFI for a shared native algorithm.
Plugin Boundaries
A good plugin keeps platform details behind a small Dart API. The API should use types that the standard message codec can represent, such as strings, numbers, booleans, lists, and maps. Native code can then return values or structured errors across the platform channel.
Keep the channel name and method names private to the plugin when possible. Application code should call meaningful Dart methods rather than invoke channels directly. This makes the API easier to test and lets you change native details without breaking users.
Exercise
Exercise
You need to format dates in a Flutter app, read the device battery level, and call an existing C image-processing library. Which package type fits each task?
Show answer
Use a Dart package for date formatting, a Flutter plugin for battery information, and an FFI package for the C image-processing library.