pigeon 10.0.1 copy "pigeon: ^10.0.1" to clipboard
pigeon: ^10.0.1 copied to clipboard

Code generator tool to make communication between Flutter and the host platform type-safe and easier.

example/README.md

Pigeon Examples #

HostApi Example #

This example gives an overview of how to use Pigeon to call into the host-platform from Flutter.

For instructions to set up your own Pigeon usage see these steps.

message.dart #

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 Book {
  String? title;
  String? author;
}

@HostApi()
abstract class BookApi {
  List<Book?> search(String keyword);
}

invocation #

This an example call to Pigeon that would ingest a defintion file pigeons/message.dart and generate corresponding output code for each supported language. (In actual use, you would not normally use both Objective-C and Swift, or both Java and Kotlin, but instead use just the languages matching your project.)

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 \
  --swift_out ios/Runner/Pigeon.swift \
  --kotlin_out android/app/src/main/kotlin/dev/flutter/pigeon/Pigeon.kt \
  --kotlin_package "dev.flutter.pigeon" \
  --java_out android/app/src/main/java/dev/flutter/pigeon/Pigeon.java \
  --java_package "dev.flutter.pigeon" \
  --cpp_header_out windows/runner/pigeon.h \
  --cpp_source_out windows/runner/pigeon.cpp \
  --cpp_namespace pigeon

AppDelegate.m #

This is the code that will use the generated Objective-C code to receive calls from Flutter.

#import "AppDelegate.h"
#import <Flutter/Flutter.h>
#import "pigeon.h"

@interface MyApi : NSObject <BookApi>
@end

@implementation MyApi
-(NSArray<Book *> *)searchKeyword:(NSString *)keyword error:(FlutterError **)error {
  Book *result = [[Book alloc] init];
  result.title =
      [NSString stringWithFormat:@"%@'s Life", request.query];
  result.author = keyword;
  return @[ result ];
}
@end

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions {
  MyApi *api = [[MyApi alloc] init];
  BookApiSetup(getFlutterEngine().binaryMessenger, api);
  return YES;
}
@end

AppDelegate.swift #

This is the code that will use the generated Swift code to receive calls from Flutter.

import Flutter

class MyApi: NSObject, BookApi {
  func search(keyword: String) -> [Book] {
    let result = Book(title: "\(keyword)'s Life", author: keyword)
    return [result]
  }
}

class AppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
  ) -> Bool {
    let api = MyApi()
    BookApiSetup.setUp(getFlutterEngine().binaryMessenger, api)
    return true
  }
}

StartActivity.java #

This is the code that will use the generated Java code to receive calls from Flutter.

import dev.flutter.pigeon.Pigeon.*;
import java.util.Collections;

public class StartActivity extends Activity {
  private class MyApi implements BookApi {
    List<Book> search(String keyword) {
      Book result = new Book();
      result.author = keyword;
      result.title = String.format("%s's Life", keyword);
      return Collections.singletonList(result)
    }
  }

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BookApi.setup(getBinaryMessenger(), new MyApi());
  }
}

test.dart #

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 {
    Api api = Api();
    List<Book?> reply = await api.search("Aaron");
    expect(reply[0].title, equals("Aaron's Life"));
  });
}

Swift / Kotlin Plugin Example #

A downloadable example of using Pigeon to create a Flutter Plugin with Swift and Kotlin can be found at gaaclarke/flutter_plugin_example.

Swift / Kotlin Add-to-app Example #

A full example of using Pigeon for add-to-app with Swift on iOS can be found at samples/add_to_app/books.

Video player plugin #

A full real-world example can also be found in the video_player plugin.

1.06k
likes
0
points
251k
downloads

Publisher

verified publisherflutter.dev

Weekly Downloads

Code generator tool to make communication between Flutter and the host platform type-safe and easier.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, args, collection, meta, path, yaml

More

Packages that depend on pigeon