This example gives an overview of how to use Pigeon to call into the host-platform from Flutter.
This is the Pigeon file that describes the interface that will be used to call from Flutter to the host-platform.
import 'package:pigeon/pigeon.dart'; class SearchRequest { String? query; } class SearchReply { String? result; } @HostApi() abstract class Api { SearchReply search(SearchRequest request); }
This is the call to Pigeon that will injest message.dart
and generate the code for iOS and Android.
flutter pub run pigeon \ --input pigeons/message.dart \ --dart_out lib/pigeon.dart \ --objc_header_out ios/Runner/pigeon.h \ --objc_source_out ios/Runner/pigeon.m \ --java_out ./android/app/src/main/java/dev/flutter/pigeon/Pigeon.java \ --java_package "dev.flutter.pigeon"
This is the code that will use the generated Objective-C code to recieve calls from Flutter.
#import "AppDelegate.h" #import <Flutter/Flutter.h> #import "pigeon.h" @interface MyApi : NSObject <Api> @end @implementation MyApi -(SearchReply*)search:(SearchRequest*)request error:(FlutterError **)error { SearchReply *reply = [[SearchReply alloc] init]; reply.result = [NSString stringWithFormat:@"Hi %@!", request.query]; return reply; } @end - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions { MyApi *api = [[MyApi alloc] init]; ApiSetup(getFlutterEngine().binaryMessenger, api); return YES; }
This is the code that will use the generated Java code to receive calls from Flutter.
import dev.flutter.pigeon.Pigeon; public class StartActivity extends Activity { private class MyApi extends Pigeon.Api { Pigeon.SearchReply search(Pigeon.SearchRequest request) { Pigeon.SearchReply reply = new Pigeon.SearchReply(); reply.result = String.format("Hi %s!", request.query); return reply; } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Pigeon.SetupApi(getBinaryMessenger(), new MyApi()); } }
This is the Dart code that will call into the host-platform using the generated Dart code.
import 'pigeon.dart'; void main() { testWidgets("test pigeon", (WidgetTester tester) async { SearchRequest request = SearchRequest()..query = "Aaron"; Api api = Api(); SearchReply reply = await api.search(request); expect(reply.result, equals("Hi Aaron!")); }); }
A downloadable example of using Pigeon to create a Flutter Plugin with Swift and Kotlin can be found at gaaclarke/flutter_plugin_example.
A full example of using Pigeon for add-to-app with Swift on iOS can be found at samples/add_to_app/books.