Sub topic 2
Creating the plugin project
This chapter assumes you can write Dart and build Flutter apps. You should also have a working Flutter toolchain, Android Studio, and Xcode. Basic Kotlin, Swift, Gradle, and CocoaPods knowledge will help when you inspect the generated native projects.
Choose a plugin instead of an app package
A Flutter plugin is a Dart package with native code for one or more platforms. Use one when Dart needs an operating-system API or a native library, such as battery information, Bluetooth, sensors, or a payment SDK.
A plain Dart package contains only Dart code. An FFI package calls native functions through a C-compatible interface. A plugin uses Flutter platform channels or generated channel code to communicate with Kotlin, Swift, Java, or Objective-C code.
Generate the project
The Flutter CLI can create the package structure and example app:
flutter create --template=plugin --platforms=android,ios -a kotlin -i swift --org com.example battery_info
cd battery_info/example
flutter runThis command creates a plugin named battery_info. The --platforms option limits the initial project to Android and iOS. The -a kotlin and -i swift options select Kotlin for Android and Swift for iOS. The --org value becomes part of the native package and bundle identifiers.
The example directory contains a small Flutter application that depends on the plugin from the parent directory. Running it on a connected device or simulator checks that the generated registration code works before you add your own behavior.
Understand the generated layout
The important directories are:
| Path | Purpose |
|---|---|
| `lib/` | Public Dart API for applications using the plugin |
| `android/` | Android library and Kotlin implementation |
| `ios/` | iOS framework configuration and Swift implementation |
| `example/` | Flutter application used to run and debug the plugin |
| `test/` | Dart tests for the package |
The root pubspec.yaml identifies the package and declares its Flutter plugin metadata. That metadata tells Flutter which native class to register on each platform. The next chapter examines this configuration in detail.
The generated Dart file usually contains a placeholder method. Replace that placeholder with the API that callers will use. Keep platform details inside the plugin. An application should call a method such as getBatteryLevel() rather than know the channel name or native method string.
Check the scaffold before editing
Run these checks from the project root:
flutter pub get
flutter analyze
flutter testIf flutter run fails, first confirm that a device or simulator is available and that the Android and iOS toolchains pass flutter doctor. An iOS build also requires macOS with Xcode. Avoid renaming the package or native classes casually after generation. Those names appear in metadata and native source files, so inconsistent changes can prevent plugin registration.
Exercise
Which command option selects Swift for the iOS implementation, and where do you run the generated example app?
Show answer
The -i swift option selects Swift. Run the example app from the example directory with flutter run.