Sub topic 4
Describing Data: Classes, Enums, and Nullable Fields
Assume you know basic Dart syntax, Flutter project structure, async and Future, and that platform channels connect Dart with Kotlin or Swift. This chapter focuses on the data schema that Pigeon uses to generate matching types on each side of that connection.
The schema is the shared contract
A Pigeon schema is a Dart file that describes values exchanged across the platform boundary. Pigeon reads this file and generates equivalent classes and conversion code for Dart, Kotlin, and Swift.
A schema can contain:
- Data classes for grouped values.
- Enums for a fixed set of choices.
- Nullable fields for values that may be absent.
- API methods that use these types as arguments or results.
The generated code is the implementation of the contract. App code should use the generated types instead of manually constructing maps or casting values from a channel response.
Data classes
A data class groups related fields into one value. Each field has a declared type.
import 'package:pigeon/pigeon.dart';
@ConfigurePigeon(PigeonOptions(
dartOut: 'lib/src/messages.g.dart',
kotlinOut: 'android/app/src/main/kotlin/com/example/app/Messages.g.kt',
kotlinOptions: KotlinOptions(package: 'com.example.app'),
swiftOut: 'ios/Runner/Messages.g.swift',
))
enum BatteryState { charging, discharging, full, unknown }
class BatteryInfo {
BatteryInfo({required this.level, required this.state});
int level;
BatteryState state;
String? source;
}BatteryInfo becomes a generated data type in each target language. The level field is required and uses a non-null integer. The state field is a non-null enum. The source field is nullable, so it may contain a string or no value.
Use field names that describe the meaning of the value. A schema is an interface shared by multiple languages, so changing a field name or type can require changes on every platform.
Required and nullable fields
A non-nullable field must have a value:
int level;
BatteryState state;A nullable field uses ?:
String? source;
int? temperature;The generated languages represent nullability using their own conventions. Kotlin uses nullable types such as String?. Swift uses optional types such as String?. Dart code must check a nullable value before treating it as present.
Future<void> describeBattery(BatteryInfo info) async {
final source = info.source ?? 'unknown source';
print('${info.level}% from $source');
}Do not use a special string such as "none" to represent missing data when the field is naturally optional. Nullability keeps “missing” separate from an actual string value.
Enums represent a closed set
An enum represents one choice from a known set:
enum BatteryState { charging, discharging, full, unknown }Generated code maps these named values between Dart, Kotlin, and Swift. Use enums when callers should choose from defined states rather than pass arbitrary text.
The unknown value is often useful when a platform cannot determine the state. It is different from null:
unknownmeans the state was checked but is not known.nullmeans no state value was supplied.
Add enum values deliberately. Native code may need an update whenever the schema gains a new choice. Avoid depending on the numeric position of an enum member because generated representations can differ across languages.
Common schema errors
| Error | Why it causes trouble | Better choice |
|---|---|---|
| Using `Map<String, dynamic>` for every result | Removes compile-time field checks | Define a data class |
| Encoding missing values as empty strings | Hides the difference between empty and absent | Use a nullable field |
| Renaming a shared field casually | Breaks generated callers and implementations | Treat names as API surface |
| Adding arbitrary enum text | Allows unsupported states | Use an enum or document free-form text |
Pigeon supports a defined set of primitive types, collections, data classes, and enums. Some combinations, especially deeply nested nullable collections, can have restrictions that depend on the Pigeon version and target language. Check the version’s supported-type documentation before designing a complex schema.
Exercise
In the following schema, which fields can be absent, and how is that different from the enum value unknown? class Reading { int value; String? unit; ReadingState state; } where ReadingState contains valid and unknown.
Show answer
Only unit can be absent because it is nullable. state must always contain an enum value. unknown is an explicit state, not an absent value.