mirror of
https://github.com/flutter/samples.git
synced 2026-05-03 04:58:50 +00:00
Added iOS example for add-to-app Pigeon (#722)
This commit is contained in:
@@ -16,10 +16,13 @@ in `pigeon/schema.dart` is updated, the generated classes can also be re-
|
||||
generated using:
|
||||
|
||||
```shell
|
||||
flutter pub run pigeon \
|
||||
--input pigeon/schema.dart \
|
||||
--java_out ../android_books/app/src/main/java/dev/flutter/example/books/Api.java \
|
||||
--java_package "dev.flutter.example.books"
|
||||
flutter pub run pigeon --input pigeon/schema.dart \
|
||||
--dart_out lib/api.dart \
|
||||
--objc_header_out ../ios_books/IosBooks/api.h \
|
||||
--objc_source_out ../ios_books/IosBooks/api.m \
|
||||
--objc_prefix BK \
|
||||
--java_out ../android_books/app/src/main/java/dev/flutter/example/books/Api.java \
|
||||
--java_package "dev.flutter.example.books"
|
||||
```
|
||||
|
||||
## Demonstrated concepts
|
||||
|
||||
@@ -1,38 +1,42 @@
|
||||
// Autogenerated from Pigeon (v0.1.0), do not edit directly.
|
||||
// Autogenerated from Pigeon (v0.1.17), do not edit directly.
|
||||
// See also: https://pub.dev/packages/pigeon
|
||||
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import
|
||||
// @dart = 2.8
|
||||
import 'dart:async';
|
||||
import 'dart:typed_data' show Uint8List, Int32List, Int64List, Float64List;
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class Book {
|
||||
String title;
|
||||
String subtitle;
|
||||
String author;
|
||||
String description;
|
||||
String summary;
|
||||
String publishDate;
|
||||
int pageCount;
|
||||
|
||||
// ignore: unused_element
|
||||
Map<dynamic, dynamic> _toMap() {
|
||||
final Map<dynamic, dynamic> pigeonMap = <dynamic, dynamic>{};
|
||||
Object encode() {
|
||||
final Map<Object, Object> pigeonMap = <Object, Object>{};
|
||||
pigeonMap['title'] = title;
|
||||
pigeonMap['subtitle'] = subtitle;
|
||||
pigeonMap['author'] = author;
|
||||
pigeonMap['description'] = description;
|
||||
pigeonMap['summary'] = summary;
|
||||
pigeonMap['publishDate'] = publishDate;
|
||||
pigeonMap['pageCount'] = pageCount;
|
||||
return pigeonMap;
|
||||
}
|
||||
|
||||
// ignore: unused_element
|
||||
static Book _fromMap(Map<dynamic, dynamic> pigeonMap) {
|
||||
final Book result = Book();
|
||||
result.title = pigeonMap['title'];
|
||||
result.subtitle = pigeonMap['subtitle'];
|
||||
result.author = pigeonMap['author'];
|
||||
result.description = pigeonMap['description'];
|
||||
result.publishDate = pigeonMap['publishDate'];
|
||||
result.pageCount = pigeonMap['pageCount'];
|
||||
return result;
|
||||
static Book decode(Object message) {
|
||||
final Map<Object, Object> pigeonMap = message as Map<Object, Object>;
|
||||
return Book()
|
||||
..title = pigeonMap['title'] as String
|
||||
..subtitle = pigeonMap['subtitle'] as String
|
||||
..author = pigeonMap['author'] as String
|
||||
..summary = pigeonMap['summary'] as String
|
||||
..publishDate = pigeonMap['publishDate'] as String
|
||||
..pageCount = pigeonMap['pageCount'] as int;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,59 +44,71 @@ abstract class FlutterBookApi {
|
||||
void displayBookDetails(Book arg);
|
||||
static void setup(FlutterBookApi api) {
|
||||
{
|
||||
const BasicMessageChannel<dynamic> channel = BasicMessageChannel<dynamic>(
|
||||
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
|
||||
'dev.flutter.pigeon.FlutterBookApi.displayBookDetails',
|
||||
StandardMessageCodec());
|
||||
channel.setMessageHandler((dynamic message) async {
|
||||
final Map<dynamic, dynamic> mapMessage =
|
||||
message as Map<dynamic, dynamic>;
|
||||
final Book input = Book._fromMap(mapMessage);
|
||||
api.displayBookDetails(input);
|
||||
});
|
||||
if (api == null) {
|
||||
channel.setMessageHandler(null);
|
||||
} else {
|
||||
channel.setMessageHandler((Object message) async {
|
||||
if (message == null) {
|
||||
return;
|
||||
}
|
||||
final Book input = Book.decode(message);
|
||||
api.displayBookDetails(input);
|
||||
return;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class HostBookApi {
|
||||
Future<void> cancel() async {
|
||||
const BasicMessageChannel<dynamic> channel = BasicMessageChannel<dynamic>(
|
||||
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
|
||||
'dev.flutter.pigeon.HostBookApi.cancel', StandardMessageCodec());
|
||||
|
||||
final Map<dynamic, dynamic> replyMap = await channel.send(null);
|
||||
final Map<Object, Object> replyMap =
|
||||
await channel.send(null) as Map<Object, Object>;
|
||||
if (replyMap == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
details: null);
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
details: null,
|
||||
);
|
||||
} else if (replyMap['error'] != null) {
|
||||
final Map<dynamic, dynamic> error = replyMap['error'];
|
||||
final Map<Object, Object> error =
|
||||
replyMap['error'] as Map<Object, Object>;
|
||||
throw PlatformException(
|
||||
code: error['code'],
|
||||
message: error['message'],
|
||||
details: error['details']);
|
||||
code: error['code'] as String,
|
||||
message: error['message'] as String,
|
||||
details: error['details'],
|
||||
);
|
||||
} else {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> finishEditingBook(Book arg) async {
|
||||
final Map<dynamic, dynamic> requestMap = arg._toMap();
|
||||
const BasicMessageChannel<dynamic> channel = BasicMessageChannel<dynamic>(
|
||||
final Object encoded = arg.encode();
|
||||
const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>(
|
||||
'dev.flutter.pigeon.HostBookApi.finishEditingBook',
|
||||
StandardMessageCodec());
|
||||
|
||||
final Map<dynamic, dynamic> replyMap = await channel.send(requestMap);
|
||||
final Map<Object, Object> replyMap =
|
||||
await channel.send(encoded) as Map<Object, Object>;
|
||||
if (replyMap == null) {
|
||||
throw PlatformException(
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
details: null);
|
||||
code: 'channel-error',
|
||||
message: 'Unable to establish connection on channel.',
|
||||
details: null,
|
||||
);
|
||||
} else if (replyMap['error'] != null) {
|
||||
final Map<dynamic, dynamic> error = replyMap['error'];
|
||||
final Map<Object, Object> error =
|
||||
replyMap['error'] as Map<Object, Object>;
|
||||
throw PlatformException(
|
||||
code: error['code'],
|
||||
message: error['message'],
|
||||
details: error['details']);
|
||||
code: error['code'] as String,
|
||||
message: error['message'] as String,
|
||||
details: error['details'],
|
||||
);
|
||||
} else {
|
||||
// noop
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ class _BookDetailState extends State<BookDetail> {
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
book.description,
|
||||
book.summary,
|
||||
style: TextStyle(color: Colors.grey.shade600, height: 1.24),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -8,7 +8,7 @@ class Book {
|
||||
String title;
|
||||
String subtitle;
|
||||
String author;
|
||||
String description;
|
||||
String summary;
|
||||
String publishDate;
|
||||
int pageCount;
|
||||
// Thumbnail thumbnail;
|
||||
|
||||
@@ -28,14 +28,14 @@ packages:
|
||||
name: async
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.5.0-nullsafety.3"
|
||||
version: "2.5.0"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: boolean_selector
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.3"
|
||||
version: "2.1.0"
|
||||
build:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -63,14 +63,14 @@ packages:
|
||||
name: characters
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.5"
|
||||
version: "1.1.0"
|
||||
charcode:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: charcode
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.3"
|
||||
version: "1.2.0"
|
||||
cli_util:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -84,7 +84,7 @@ packages:
|
||||
name: clock
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.3"
|
||||
version: "1.1.0"
|
||||
code_builder:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -98,7 +98,7 @@ packages:
|
||||
name: collection
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.15.0-nullsafety.5"
|
||||
version: "1.15.0"
|
||||
convert:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -126,7 +126,7 @@ packages:
|
||||
name: fake_async
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.3"
|
||||
version: "1.2.0"
|
||||
file:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -185,14 +185,14 @@ packages:
|
||||
name: matcher
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.12.10-nullsafety.3"
|
||||
version: "0.12.10"
|
||||
meta:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: meta
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0-nullsafety.6"
|
||||
version: "1.3.0"
|
||||
mockito:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
@@ -227,7 +227,7 @@ packages:
|
||||
name: path
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.8.0-nullsafety.3"
|
||||
version: "1.8.0"
|
||||
pedantic:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -274,56 +274,56 @@ packages:
|
||||
name: source_span
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.8.0-nullsafety.4"
|
||||
version: "1.8.0"
|
||||
stack_trace:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stack_trace
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.10.0-nullsafety.6"
|
||||
version: "1.10.0"
|
||||
stream_channel:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: stream_channel
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.3"
|
||||
version: "2.1.0"
|
||||
string_scanner:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: string_scanner
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.1.0-nullsafety.3"
|
||||
version: "1.1.0"
|
||||
term_glyph:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: term_glyph
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.2.0-nullsafety.3"
|
||||
version: "1.2.0"
|
||||
test_api:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: test_api
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.2.19-nullsafety.6"
|
||||
version: "0.2.19"
|
||||
typed_data:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: typed_data
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "1.3.0-nullsafety.5"
|
||||
version: "1.3.0"
|
||||
vector_math:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vector_math
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "2.1.0-nullsafety.5"
|
||||
version: "2.1.0"
|
||||
watcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -339,4 +339,4 @@ packages:
|
||||
source: hosted
|
||||
version: "2.2.1"
|
||||
sdks:
|
||||
dart: ">=2.12.0-0.0 <3.0.0"
|
||||
dart: ">=2.12.0 <3.0.0"
|
||||
|
||||
8
add_to_app/books/flutter_module_books/run_pigeon.sh
Executable file
8
add_to_app/books/flutter_module_books/run_pigeon.sh
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
flutter pub run pigeon --input pigeon/schema.dart \
|
||||
--dart_out lib/api.dart \
|
||||
--objc_header_out ../ios_books/IosBooks/api.h \
|
||||
--objc_source_out ../ios_books/IosBooks/api.m \
|
||||
--objc_prefix BK \
|
||||
--java_out ../android_books/app/src/main/java/dev/flutter/example/books/Api.java \
|
||||
--java_package "dev.flutter.example.books"
|
||||
Reference in New Issue
Block a user