What does the Pigeon package do?
Pigeon takes a Dart API definition file and generates strongly typed native code for Android, iOS, and other platforms along with Dart bindings.
Build platform-aware interfaces and generate type-safe platform channel code with Pigeon
You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.
Having trouble? AI is here to help
Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.
Use the AI button at the bottom of the screen
Flutter apps run on Android, iOS, web, desktop, and more, but users on each platform expect the interface to follow their platform conventions. Android users expect Material Design components like floating action buttons, bottom navigation bars, and snack bars. iOS users expect Cupertino-style components like sliding segmented controls, tab bars at the bottom, and action sheets. Flutter provides both Material and Cupertino widget libraries so you can build platform-adaptive interfaces that feel native on every platform. You can detect the current platform at runtime using Theme.of(context).platform or the Platform class from dart:io and render the appropriate widget for each platform. Beyond UI adaptation, if you need to call native platform APIs, the Pigeon package generates type-safe platform channel code that eliminates the error-prone string matching of raw MethodChannel calls. You define your API in a Dart file, run the Pigeon code generator, and it produces strongly typed Kotlin, Swift, C++, or Objective-C code for the native side along with the corresponding Dart bindings. This ensures that method names, argument types, and return types are always in sync between Dart and native code.
1// ── Platform-Adaptive Widgets ──2import 'dart:io' show Platform;3import 'package:flutter/cupertino.dart';4import 'package:flutter/material.dart';56// Adaptive dialog that uses Material on Android, Cupertino on iOS7Future<void> showAdaptiveAlert(BuildContext context, String message) {8if (Platform.isIOS) {9return showCupertinoDialog(10context: context,11builder: (_) => CupertinoAlertDialog(12title: const Text('Alert'),13content: Text(message),14actions: [15CupertinoDialogAction(16child: const Text('OK'),17onPressed: () => Navigator.pop(context),18),19],20),21);22}23return showDialog(24context: context,25builder: (_) => AlertDialog(26title: const Text('Alert'),27content: Text(message),28actions: [29TextButton(30onPressed: () => Navigator.pop(context),31child: const Text('OK'),32),33],34),35);36}3738// Adaptive switch using Flutter built-in adaptive constructor39Switch.adaptive(40value: isDarkMode,41onChanged: (val) => setState(() => isDarkMode = val),42)4344// Adaptive indicator45CircularProgressIndicator.adaptive()4647// Platform-aware scaffold pattern48class AdaptiveScaffold extends StatelessWidget {49final Widget body;50final String title;51const AdaptiveScaffold({super.key, required this.body, required this.title});5253@override54Widget build(BuildContext context) {55if (Platform.isIOS) {56return CupertinoPageScaffold(57navigationBar: CupertinoNavigationBar(middle: Text(title)),58child: SafeArea(child: body),59);60}61return Scaffold(62appBar: AppBar(title: Text(title)),63body: body,64);65}66}
The Pigeon package generates type-safe platform channel code from a simple Dart definition file. Instead of passing raw strings and dynamic values through MethodChannel, you define structured request and response classes along with an abstract API interface. Running the Pigeon code generator produces native code for Android (Kotlin or Java), iOS (Swift or Objective-C), Windows (C++), and macOS, along with the Dart bindings. The generated code handles serialization, deserialization, and error handling automatically, so you never have to worry about mismatched method names or data types.
1// ── Pigeon Setup ──2// pubspec.yaml (dev_dependencies):3// pigeon: ^22.0.045// ── Step 1: Define the API in a Dart file ──6// pigeons/messages.dart7import 'package:pigeon/pigeon.dart';89class SearchRequest {10final String query;11final int maxResults;12SearchRequest({required this.query, required this.maxResults});13}1415class SearchResult {16final String title;17final String url;18SearchResult({required this.title, required this.url});19}2021// @HostApi means Flutter calls native code22@HostApi()23abstract class SearchApi {24@async25List<SearchResult> search(SearchRequest request);26}2728// @FlutterApi means native code calls Flutter29@FlutterApi()30abstract class NotificationApi {31void onNotificationReceived(String message);32}3334// ── Step 2: Generate code ──35// dart run pigeon --input pigeons/messages.dart3637// This generates:38// - android/app/src/main/kotlin/.../Messages.g.kt39// - ios/Runner/Messages.g.swift40// - lib/messages.g.dart4142// ── Step 3: Use generated Dart code ──43class SearchService {44final SearchApi _api = SearchApi();4546Future<List<SearchResult>> search(String query) async {47final request = SearchRequest(query: query, maxResults: 10);48return await _api.search(request);49}50}5152// ── Step 4: Implement native side (Kotlin example) ──53// class SearchApiImpl : SearchApi {54// override fun search(55// request: SearchRequest,56// callback: (Result<List<SearchResult>>) -> Unit57// ) {58// val results = performNativeSearch(request.query, request.maxResults)59// callback(Result.success(results))60// }61// }62//63// // Register in MainActivity64// SearchApi.setUp(flutterEngine.dartExecutor, SearchApiImpl())
Flutter provides several widgets with an .adaptive constructor that automatically switch between Material and Cupertino styles based on the platform. These include Switch.adaptive, Slider.adaptive, CircularProgressIndicator.adaptive, and AlertDialog.adaptive. Use these whenever possible to reduce platform-specific code while still providing a native feel on each platform.
Adaptation should preserve product consistency while respecting platform expectations:
The goal is native feel without maintaining two unrelated app experiences.
Build platform aware interfaces and generate type safe platform channel code with Pigeon. Forms collect user input, and validation ensures the input is safe and useful.
Think in architecture, reliability, and team-level maintainability.
Show validation messages early and clearly, near the field that needs correction.
Most app trust problems start when invalid or incomplete input reaches backend systems.
Validated input at UI and domain layers protects business logic from invalid assumptions.
As your app grows, keep UI, state, and data responsibilities separate so each part stays testable and easier to modify.
Pigeon takes a Dart API definition file and generates strongly typed native code for Android, iOS, and other platforms along with Dart bindings.
The @HostApi annotation indicates that the API is implemented on the native (host) side and called from the Flutter (client) side.
The Platform class from dart:io provides boolean properties like isIOS, isAndroid, isWindows, and others to detect the current platform at runtime.
Something seems incorrect, unclear, or missing? Let us know.
FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.
Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.
Sign in to continue with exercises and quiz for this lesson.
Sign In to Continue