Sub topic 3
Telling Flutter About Your Native Code in `pubspec.yaml`
This chapter assumes you can write Dart and build Flutter apps. You should also know the basics of Kotlin, Swift, and YAML. The previous chapter created a plugin project. Here, you will connect that project’s native classes to Flutter’s plugin configuration.
The role of the plugin section
A Flutter plugin has Dart code and one or more native implementations. The flutter.plugin.platforms section in pubspec.yaml tells the Flutter tool which native class belongs to each supported platform.
Flutter uses this information when it builds the application and registers the plugin with the engine.
name: battery_info
description: Reads the battery level on Android and iOS.
version: 0.0.1
environment:
sdk: ^3.0.0
flutter: ">=3.10.0"
dependencies:
flutter:
sdk: flutter
flutter:
plugin:
platforms:
android:
package: com.example.battery_info
pluginClass: BatteryInfoPlugin
ios:
pluginClass: BatteryInfoPluginThe package value is required for Android. It is the Kotlin or Java package containing the plugin class. The pluginClass value is the native class name. On iOS, pluginClass names the Swift or Objective-C class that implements FlutterPlugin.
The Dart package name, Android package name, and native class name have different purposes:
| Configuration | Example | Purpose |
|---|---|---|
| Dart package name | `battery_info` | Used in Dart imports |
| Android package | `com.example.battery_info` | Identifies the Android namespace |
| Native plugin class | `BatteryInfoPlugin` | Receives registration and channel calls |
Matching configuration to source files
The configuration must agree with the declarations in your native files.
For Android, the Kotlin file should begin with the same package name:
package com.example.battery_info
class BatteryInfoPlugin : FlutterPlugin, MethodCallHandler {
// Channel setup and method handling go here.
}The class name must match pluginClass exactly, including capitalization. A class named BatteryPlugin will not satisfy a configuration that names BatteryInfoPlugin.
For iOS, the class must be visible to the plugin registrar:
import Flutter
public class BatteryInfoPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
// Create the method channel and register the delegate.
}
}The iOS configuration does not need a package value because Swift uses modules and targets rather than Android-style package names.
Validating the declaration
Run these commands from the plugin root after changing pubspec.yaml:
flutter pub get
flutter analyzeflutter pub get reads the package metadata and refreshes generated plugin information. flutter analyze checks the Dart side, but it does not replace an Android or iOS build. Test each native target through the example app because a spelling error can appear only during platform compilation.
Exercise
What is the difference between the Android package value and the pluginClass value?
Show answer
package identifies the Kotlin or Java namespace that contains the native class. pluginClass identifies the class Flutter should register inside that namespace.