Skip to content

Pigeon : [FlutterApi] Support for asynchronous methods/Functions #64810

Description

@xsahil03x

@gaaclarke

Issue: Currently there is no way to define async methods in (FlutterApi) while generating code using Pigeon.

@FlutterApi()
abstract class TestFlutterApi {
  UserResult getUser(); // I want this to be async
}

This code here generates

abstract class TestFlutterApi {
  UserResult getUser();
  static void setup(TestFlutterApi api) {
    {
      const BasicMessageChannel<dynamic> channel =
          BasicMessageChannel<dynamic>('dev.flutter.pigeon.TestFlutterApi.getUser', StandardMessageCodec());
      channel.setMessageHandler((dynamic message) async {
        final UserResult output = api.getUser();
        return output._toMap();
      });
    }
  }
}

Solution: The solution is pretty simple, we just need to somehow tell the generator that this function is an async one. We can do this easily using an annotation to mark it such and just add an await keyword in front of the function execution while code generation.

@FlutterApi()
abstract class TestFlutterApi {
  @async
  UserResult getUser();
}

This will somehow generate

abstract class TestFlutterApi {
  Future<UserResult> getUser();
  static void setup(TestFlutterApi api) {
    {
      const BasicMessageChannel<dynamic> channel =
          BasicMessageChannel<dynamic>('dev.flutter.pigeon.TestFlutterApi.getUser', StandardMessageCodec());
      channel.setMessageHandler((dynamic message) async {
        final UserResult output = await api.getUser();
        return output._toMap();
      });
    }
  }
}

Activity

  1. gaaclarke commented on Aug 28, 2020

    @gaaclarke
    Member

    Duplicate of #60399

    Check out that issue for a workaround and to see what we were planning on implementing for this in the future. Thanks!

  2. github-actions commented on Aug 13, 2021

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

  3. locked as resolved and limited conversation to collaborators on Aug 13, 2021
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions