Sub topic 9
Running the Plugin from the Example App
Assume you can build a Flutter app and have Android Studio, Xcode, and a working Flutter toolchain installed. The example app is the fastest way to run a plugin against real platform code before publishing it.
Understand the Example App
A plugin project contains two related packages:
- The plugin package contains the Dart API and native Android and iOS implementations.
- The
examplepackage is a small Flutter application that depends on the plugin by local path.
The example app's pubspec.yaml usually contains a dependency like this:
dependencies:
flutter:
sdk: flutter
battery_info:
path: ../The path value points to the plugin package in the parent directory. This lets the app use your current source files without publishing a package or downloading a hosted version.
The application starts in example/lib/main.dart. Use it to call the public Dart API and display the result:
import 'package:battery_info/battery_info.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const BatteryApp());
}
class BatteryApp extends StatelessWidget {
const BatteryApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Battery Info')),
body: FutureBuilder<int>(
future: BatteryInfo().getBatteryLevel(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
}
if (!snapshot.hasData) {
return const Center(child: CircularProgressIndicator());
}
return Center(
child: Text('Battery: ${snapshot.data}%'),
);
},
),
),
);
}
}FutureBuilder rebuilds the widget when the asynchronous platform call completes. It also gives the example app a visible place to inspect errors.
Run on Android and iOS
From the plugin's root directory, fetch dependencies and move into the example application:
flutter pub get
cd example
flutter devices
flutter run -d <device-id>flutter devices lists connected phones, emulators, and simulators. Replace <device-id> with one of the listed identifiers. You can also run flutter run if Flutter finds only one suitable device.
The call travels through the project like this:
Debug Common Failures
| Symptom | Likely cause | Check |
|---|---|---|
| Plugin is not found | Dependencies were not refreshed | Run `flutter pub get` in `example` |
| `MissingPluginException` | Native code was not registered or the app was hot-reloaded after native changes | Stop the app and run it again |
| Android build fails | Android SDK, Gradle, or Kotlin setup is incompatible | Run `flutter doctor -v` and inspect the first build error |
| iOS build fails | Pods or signing settings are stale | Run `pod install` in `example/ios`, then rebuild |
When debugging, place breakpoints in both example/lib/main.dart and the native handler. Confirm that the Dart method name, channel name, and native method name match exactly. Test unavailable hardware states as well as successful results.
Exercise
You changed the Kotlin handler, but the running app still behaves as before. What should you do first?
Show answer
Stop the application and run it again. A full rebuild reloads native plugin code that hot reload may leave unchanged.