Skip to content

[pigeon] Support typesafe EventChannels #66711

Closed
Closed

Description

@ryanheise

Use case

It would be useful to have typesafe event channels in pigeon as these currently have to be created manually (and type safety has to be ensured manually).

Proposal

Automatically translate Stream getters to event channels:

@HostApi()
abstract class VideoPlayerApi {
  // The following getter represents the new feature:
  Stream<VideoEvent> get videoEvents;
  void dispose();
  void setLooping(LoopingMessage msg);
  void setVolume(VolumeMessage msg);
  void play();
  PositionMessage position();
  void seekTo(PositionMessage msg);
  void pause();
  void setMixWithOthers(MixWithOthersMessage msg);
}

The above example is based on #66710 where an instance of this API represents a single instance of a video player, and so the videoEvents stream would map to an event channel for a particular video player instance.

Activity

  1. ryanheise commented on Sep 26, 2020

    @ryanheise
    Author
  2. mirzaeiyan commented on Jul 4, 2021

    @mirzaeiyan
  3. added
    P3Issues that are less important to the Flutter project
    on Aug 16, 2021
  4. Adherentman commented on Apr 25, 2022

    @Adherentman
  5. gaaclarke commented on Apr 25, 2022

    @gaaclarke
    Member

    No update. Just waiting for better signal that it is worth the investment to make the case to management.

  6. claudius-kienle commented on May 26, 2022

    @claudius-kienle
  7. c15yi commented on May 31, 2022

    @c15yi
  8. brianquinlan commented on Jul 20, 2022

    @brianquinlan
    Contributor

    I'd love it too. A good first step would be to generate code for classes that don't appear in the @HostAPI class so that the user can manually serialize them to dicts and send them over the EventChannel.

  9. grantsavill commented on Sep 16, 2022

    @grantsavill
  10. stuartmorgan-g commented on Jan 22, 2023

    @stuartmorgan-g
    Contributor
  11. 18 remaining items

  12. Abson commented on Dec 24, 2024

    @Abson
  13. tttocklll commented on Dec 26, 2024

    @tttocklll

    I discovered that this feature has been supported since version 22.7.0 🎉.
    I have tried it and confirmed that it works well!

    Here's an example of how we can use it:

    class ExampleEventData {
      ExampleEventData({
        required this.data,
      });
    
      final String data;
    }
    
    @EventChannelApi()
    abstract class ExampleEventChannelApi {
      ExampleEventData exampleStreamEvents();
    }
    
    // -----
    
    Stream<ExampleEventData> getExampleEventStream() async* {
      final Stream<ExampleEventData> events = exampleStreamEvents();
      await for (final ExampleEventData event in events) {
        yield event;
      }
    }

    You can refer to the official example here: https://github.com/flutter/packages/blob/main/packages/pigeon/example/README.md#event-channel-example

  14. tarrinneal commented on Dec 27, 2024

    @tarrinneal
    Contributor

    I guess I should have mentioned it here when I added it. Kotlin and Swift support Event Channels now. I haven't closed this issue since it hasn't been added to all languages yet. I might close it and open a new one since that will notify people that it is partially complete.

  15. tarrinneal commented on Dec 27, 2024

    @tarrinneal
    Contributor

    I discovered that this feature has been supported since version 22.7.0 🎉. I have tried it and confirmed that it works well!

    Thanks for the feedback! Let me know if you run into any issues, or features that you think it could use!

  16. time4tea commented on Jan 7, 2025

    @time4tea

    Thanks for this change - it is exactly what i need! - I did find a case though that would be awesome if you could take into consideration.

    in case of the following pigeon

    sealed class BobEvent {}
    
    class BobStart extends BobEvent {
      BobStart();
    }

    the following kotlin is generated, which is invalid - data classes must have a member, and so this should be just a class I think

    sealed class BobEvent 
    
    /** Generated class from Pigeon that represents data sent in messages. */
    data class BobStart (
    ) : BobEvent()
     {
      companion object {
        fun fromList(pigeonVar_list: List<Any?>): BobStart {
          return BobStart()
        }
      }
      fun toList(): List<Any?> {
        return listOf(
        )
      }
    }
  17. stuartmorgan-g commented on Jan 7, 2025

    @stuartmorgan-g
    Contributor

    #160501 tracks supporting empty classes.

  18. time4tea commented on Jan 7, 2025

    @time4tea

    for now i am just doing this, which works - in case this is useful... while the related PR is in the pipes.

    sealed class BobEvent {}
    
    class BobStart extends BobEvent {
      bool? unused;
      BobStart();
    }

    which generates

    data class BobStart (
      val unused: Boolean? = null
    ) : BobEvent()
     { ... }
  19. tarrinneal commented on Jan 15, 2025

    @tarrinneal
    Contributor

    Closing as completed with flutter/packages#7892.

    Support for other languages/platforms can be tracked here: #161633

  20. github-actions commented on Jan 29, 2025

    @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.

  21. locked as resolved and limited conversation to collaborators on Jan 29, 2025
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

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions