Skip to content

[pigeon] Support static constant values #66518

Closed

Description

@bsutton

I'm looking to define constant values to represent error codes.

I can't find any documentation on what happens to any constants that are defined and where they should be defined.

I'm going to try and add them to the class defined for @HostApi .

This does seem to suggest that the documentation should cover off what is permitted in the HostApi and FlutterApi classes.

Activity

  1. added
    p: pigeonrelated to pigeon messaging codegen tool
    on Sep 24, 2020
  2. bsutton commented on Sep 24, 2020

    @bsutton
    Author

    After testing it looks like constants are not supported.

    I would suggest this is a pretty important implementation detail otherwise we will be left to maintain matching constants list on both sides of the wire.

    Support for static constants would seem fairly straight forward and solve a large class of problems (including obviating the short term need for enum support).

    /// The collection of methods we use to call into the platform code.
    @HostApi()
    abstract class SoundsToPlatformApi {
      /// The list of specific errors that can be passed from the platform to dart.
      static const errnoGeneral = 1;
      static const errnoUnsupportedCodec = 2;
  3. bsutton commented on Oct 5, 2020

    @bsutton
    Author

    I have found a way to pass static constants through the api which avoid having to maintain mappings in the host code.

    This is still fairly ugly but provides a work around until static consts are supported.

    When creating an argument class:

    class StartCar
    {
    int carId;
    int mode;
    int const_suv_mode;
    int const_sports_mode;
    }

    You then need another 'helper' class.
    class StartCardConsts
    {
    static const suv_mode = 1;
    static const sports_mode = 2;
    }

    When constructing the argument class:

    var args = StartCar();
    args.carId = xx;
    args.mode = StartCardConsts.suv_mode;
    args.const_suv_mode = StartCardConsts.suv_mode;
    args.const_sports_mode = StartCardConsts.sports_mode;

    HostApi().startCar(args);

    The resulting java/ios code will now have getters for each of the modes so you can write

    if (args.mode == args.getConstSuvMode())
  4. added
    P3Issues that are less important to the Flutter project
    on Oct 20, 2021
  5. serverplumber commented on Aug 26, 2022

    @serverplumber
  6. changed the title [-][pigeon] are constants supported.[/-] [+][pigeon] Support static constant ints[/+] on Mar 26, 2024
  7. changed the title [-][pigeon] Support static constant ints[/-] [+][pigeon] Support static constant values[/+] on Mar 26, 2024
  8. kfieldi5 commented on Apr 6, 2024

    @kfieldi5
  9. tarrinneal commented on Jul 21, 2026

    @tarrinneal
    Contributor

    closed as fixed by flutter/packages#12032

  10. github-actions commented on Aug 4, 2026

    @github-actions

    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 -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectd: api docsIssues with https://api.flutter.dev/p: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions