Repository navigation
pigeon: implement primitive enum return values and arguments #87307
Description
Activity
- addedp: pigeonrelated to pigeon messaging codegen toolrelated to pigeon messaging codegen tool
on Jul 29, 2021 - addedpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.c: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutter
on Aug 31, 2021 - addedP3Issues that are less important to the Flutter projectIssues that are less important to the Flutter project
on Oct 12, 2021 For Dart and Android, support for this feature is pretty straightforward. It can be solved by just updating the codec with enum indexes:
Dart:
enum Fruit { apple, banana, } class ACodec extends StandardMessageCodec { const ACodec(); @override void writeValue(WriteBuffer buffer, Object? value) { if (value is Fruit) { buffer.putUint8(128); writeValue(buffer, value.index); } else { super.writeValue(buffer, value); } } @override Object? readValueOfType(int type, ReadBuffer buffer) { switch (type) { case 128: return Fruit.values[readValue(buffer)! as int]; default: return super.readValueOfType(type, buffer); } } }
Java:
public enum Fruit { apple(0), banana(1); private int index; Fruit(final int index) { this.index = index; } } private static class ACodec extends StandardMessageCodec { public static final ACodec INSTANCE = new ACodec(); private ACodec() {} @Override protected Object readValueOfType(byte type, ByteBuffer buffer) { switch (type) { case (byte)128: return Fruit.values()[(int) readValue(buffer)]; default: return super.readValueOfType(type, buffer); } } @Override protected void writeValue(ByteArrayOutputStream stream, Object value) { if (value instanceof Fruit) { stream.write(128); writeValue(stream, ((Fruit) value).index); } else { super.writeValue(stream, value); } } }
But, the codec for Objective-C doesn't handle Objective-C type enums well. e.g:
typedef NS_ENUM(NSUInteger, Fruit) { FruitApple = 0, FruitBanana = 1, };
FlutterStandardReader.readValueOfTypeandFlutterStandardWriter.writeValuerequire an object, not a primitive, to be returned or passed respectively. So, the solution won't be as straightforward as other languages. A couple of solutions I came up with are:- [Breaking Change] The Objective-C generation could create Enum like classes with static variables. e.g.
@interface Fruit : NSObject @property(readonly) NSUInteger index; - (instancetype)init NS_UNAVAILABLE; + (Fruit *)apple; + (Fruit *)banana; @end
- Methods for
HostApis/FlutterApis could take and/or return enum wrappers. e.g.
typedef NS_ENUM(NSUInteger, Fruit) { FruitApple = 0, FruitBanana = 1, }; @interface FruitEnum : NSObject @property(readonly) Fruit value; - (instancetype)initWithValue:(Fruit)value; @end @protocol AClass - (void)aMethod:(NSArray<FruitEnum *> *)fruits error:(FlutterError *_Nullable *_Nonnull)error; @end
After merging flutter/packages#1871:
-
Enumargument works ✅:void bar(Foo foo);
-
List<Enum>argument does not work ❌:void bar(List<Foo> foo);
@gaaclarke is this intended?
-
mchouchane-enedis commented
on Apr 11, 2023 on Apr 11, 2023 · Hidden as off-topicshow commentMore actions- addedP1High-priority issues at the top of the work listHigh-priority issues at the top of the work listand removedP3Issues that are less important to the Flutter projectIssues that are less important to the Flutter project
on Apr 17, 2023 - addedteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem team
on Jul 8, 2023 - added a commit that references this issue
on Aug 28, 2023 This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.- locked as resolved and limited conversation to collaborators
on Sep 11, 2023 - added a commit that references this issue
on Jun 10, 2026 - added a commit that references this issue
on Jun 19, 2026
example: