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

outdated

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 is the call to Pigeon that will ingest 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"

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

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.

1040
likes
0
pub points
96%
popularity

Publisher

verified publisherflutter.dev

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

More

Packages that depend on pigeon