Sub topic 3
Testing the Dart layer with a mocked channel
This chapter assumes a working plugin from earlier chapters, the flutter_test package, and comfort with Future and async/await.
Mocking the method channel
Dart tests run on your development machine, so no Android or iOS code runs. A mock handler replaces the native side and returns fixed values. This checks the channel name, your Dart wrapper, and error mapping without a device.
import 'package:battery_info/battery_info.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
const channel = MethodChannel('battery_info');
setUp(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall call) async {
if (call.method == 'getBatteryLevel') {
return 42;
}
return null;
});
});
tearDown(() {
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, null);
});
test('getBatteryLevel returns the value from the platform', () async {
expect(await BatteryInfo().getBatteryLevel(), 42);
});
}Keeping native work off the main thread
Android calls onMethodCall on the main thread. Run slow work on a background thread, then call result on the main thread, because Flutter requires result methods to run there.
Streaming events from native code
An EventChannel turns a native event source into a Dart Stream. Native code starts sending when the first listener subscribes.
static const EventChannel _events = EventChannel('battery_info/level_changes');
Stream<int> get onBatteryLevelChanged =>
_events.receiveBroadcastStream().map((dynamic event) => event as int);Working with the Activity and plugin lifecycle on Android
FlutterPlugin gives you an application context, not an Activity. To reach the Activity, implement ActivityAware. Clear every reference to it in onDetachedFromActivity, because the Activity can be destroyed and recreated during configuration changes.
Adding native libraries with Gradle and CocoaPods
Add Android libraries to the dependencies block of android/build.gradle. Add iOS libraries with s.dependency in the .podspec file under ios/.
Generating type-safe channel code with Pigeon
Pigeon generates matching Dart and native message classes from one Dart definition file. This removes hand-written string method names and manual type casts. Run it with dart run pigeon --input pigeons/messages.dart.
Splitting a plugin into a federated package set
A federated plugin separates the app-facing API, a platform interface, and one package per platform. Use it when a platform needs its own package or when several teams maintain different platforms.
Preparing and publishing to pub.dev
Set the version in pubspec.yaml, add a CHANGELOG.md entry, and fill in description and repository. Run flutter pub publish --dry-run first. A published version cannot be deleted or overwritten, so publish only after the dry run reports no warnings you need to fix.
Exercise
Exercise
What does the test's setUp do, and why does tearDown reset the handler to null?
Show answer
setUp registers a mock handler that returns 42 for getBatteryLevel, so the test does not need a device. tearDown removes the mock so that later tests use a clean channel state.