Sub topic 10
Supported Types and What Pigeon Cannot Express
This chapter assumes you know basic Dart types, Future, and Flutter platform channels. It also assumes you can read simple Kotlin or Swift code.
Pigeon generates a codec that converts values between Dart and native code. The safest designs use small data classes and explicit fields instead of passing loosely shaped maps.
Types Pigeon can represent
Pigeon supports common scalar values, byte data, collections, enums, and classes. The exact generated names differ by platform, but the schema remains the source of truth.
| Pigeon type | Typical native representation | Common use |
|---|---|---|
| `bool` | `Boolean` or `Bool` | Flags |
| `int` | `Long` or `Int64` | Counts and identifiers |
| `double` | `Double` | Measurements |
| `String` | `String` | Text |
| `Uint8List` | `ByteArray` or `Data` | Binary data |
| `List<T>` | List or array | Ordered values |
| `Map<K, V>` | Map or dictionary | Key-value data |
| Enum | Generated enum | Fixed choices |
| Pigeon class | Generated data class | Structured records |
Nullable values use ?. For example, String? means the value may be null. Nullability must agree across the generated Dart, Kotlin, and Swift code. A field that is required in one implementation must not be returned as null by another implementation.
A schema can combine these types:
import 'package:pigeon/pigeon.dart';
class UserProfile {
UserProfile({
required this.id,
required this.displayName,
this.avatarBytes,
required this.tags,
});
String id;
String displayName;
Uint8List? avatarBytes;
List<String> tags;
}
@HostApi()
abstract class ProfileApi {
UserProfile? loadProfile(String userId);
}Here, avatarBytes is optional binary data, and loadProfile can return no profile. The generated APIs preserve these distinctions better than a Map<String, dynamic> passed through a hand-written channel.
Nested values and collection limits
Collections should have a clear element type. A list of UserProfile values is easier to validate than a list containing unrelated objects. Maps also work best when their keys and values use supported, predictable types.
Do not use a custom Dart object, function, stream, DateTime, URI, or exception as a schema field unless you convert it first. For example, represent a date as an ISO-8601 String or as an integer timestamp. Represent a file as a path, URL, or Uint8List, depending on the operation.
What Pigeon cannot express directly
Pigeon describes messages and method calls. It does not provide a universal representation for every Dart or native feature.
Common limits include:
- Platform-specific objects such as
Activity,UIViewController,Intent, orUIImage. - Functions and Dart callbacks as ordinary field values.
- Open-ended polymorphic hierarchies, where one field may contain several unrelated class types.
- Arbitrary generic types whose concrete element types are unknown.
- Native handles that are meaningful only inside one process.
- Continuous streams as a normal method result.
- Shared mutable objects. Values cross the boundary as encoded messages.
For these cases, define a boundary representation. Send an identifier instead of a native object, send a list of records instead of a polymorphic object graph, and use explicit methods for operations on a resource.
A Future<T> or an asynchronous native method is still a method call, not a stream. If native code must send repeated updates, use an event-oriented API supported by your Pigeon version or use an EventChannel deliberately. Document who starts and stops the subscription and how errors reach Dart.
Choosing the boundary
A good Pigeon schema contains stable business data, not platform implementation details. If a native API returns a large or unstable object, select only the fields that Dart needs. This reduces coupling and makes generated-code changes easier to review.
Also check the generated output after changing a type. A Dart int may become Kotlin Long, and a nullable field may become an optional type in Swift. Use the generated signatures to guide native implementations rather than guessing the mapping.
Exercise
You need to send a camera image and its capture time to native code. Which Pigeon types could represent them, and why should you avoid sending a camera framework object?
Show answer
Use Uint8List for encoded image bytes and String or int for the capture time. A camera framework object is platform-specific and cannot be serialized as a portable Pigeon value.