Repository navigation
Description
Is it possible to generate async return values in pigeon? If not, is this a feature that could be added? This could be useful for several native plugins I'm working on.
In particular, the video_player plugin defines the create pigeon method as follows:
// messages.dart
TextureMessage create(CreateMessage msg);The Obj-C implementation becomes:
- (FLTTextureMessage*)create:(FLTCreateMessage*)input error:(FlutterError**)error {
...
return textureMessage;
}I maintain a video_player fork which adds support for PHAsset videos. My implementation creates a TextureMessage within a callback in the create method which in turn makes the TextureMessage instance unreachable when the create function returns.
I'm looking for something like the following:
// messages.dart
Future<TextureMessage> create(CreateMessage msg);Which would generate the following code on iOS:
- (void)create:(FLTCreateMessage*)input
error:(FlutterError* __strong*)error
callback:(FlutterReplyForFLTTextureMessage)callback {
...
callback(textureMessage, *error));
...
}For now, I have manually edited the pigeon generated files to overcome this limitation in my video_player fork: recastrodiaz/plugins@c228f21#diff-8e327466253211b3b37c9a42a44ba65dR230
Manually edited create method: https://github.com/recastrodiaz/plugins/blob/c228f21229c870fb01ea1e0ae4d137b146744084/packages/video_player/video_player/ios/Classes/FLTVideoPlayerPlugin.m#L884
@gaaclarke Let me know if this is possible to do or if this is something that you may consider adding.
Thanks!
Activity
gaaclarke commentedon Jun 29, 2020
It's definitely on the roadmap. The chances of me getting to it soon are slim. Here is what I had in mind, if the pigeon file declared the method
asyncthe generated methods would be async:recastrodiaz commentedon Jun 29, 2020
That would work for my use cases. I'm looking forward to this feature.
Not passing the
** errorpointer as parameter todoitis a good choice. The code in my previous message did not actually work in release mode (it was ok in debug mode). Setting the parameter as__strongseemed to have had no effect and by the time the callback function was called, the error variable pointed to an invalid address:My work around was to declare
errorwithin the block scope: recastrodiaz/plugins@a31d0ec#diff-b8b3fa6c990e0fc6b09b10abac5904caR886gaaclarke commentedon Jun 29, 2020
@recastrodiaz you should check out the Pigeon repo https://github.com/flutter/packages/tree/master/packages/pigeon, it might be something you'd want to implement ;) I'm a bit biased but I think it's pretty fun to work on.
recastrodiaz commentedon Jun 29, 2020
Alright, I'll check your code out and I'll try to come up with a proof of concept :). What should the Java Generated API interface/implementaion look like?
gaaclarke commentedon Jun 29, 2020
@recastrodiaz Yea, check out how MethodChannel's do it to get an idea since they use async methods by default: https://api.flutter.dev/javadoc/io/flutter/plugin/common/MethodChannel.html#invokeMethod-java.lang.String-java.lang.Object-io.flutter.plugin.common.MethodChannel.Result-
recastrodiaz commentedon Jun 29, 2020
Thanks. Will check that out.
gaaclarke commentedon Jul 1, 2020
@recastrodiaz I forgot to mention that you can implement asynchronous functions by creating 2 APIs as a workaround:
That might be a hint to how we could easily implement the feature, too, but I haven't thought that far.
fzyzcjy commentedon Aug 16, 2020
+1 for this feature!
fzyzcjy commentedon Sep 17, 2020
@gaaclarke Hi it has been another 1 month... Any updates? Thanks!
recastrodiaz commentedon Sep 17, 2020
Hi @fzyzcjy I got busy with work. It might still take me another month before getting to this. If someone would like to give it a go in the meantime, feel free.
fzyzcjy commentedon Sep 17, 2020
@recastrodiaz Thanks very much! I will also try to do it when having some time. (By the way, will the official flutter team do it?)
gaaclarke commentedon Sep 17, 2020
This PR started working on it: flutter/packages#201 It still doesn't handle
@HostApi. I didn't look closely at the approach yet since it is half implemented.fzyzcjy commentedon Sep 17, 2020
@gaaclarke Thanks!
gaaclarke commentedon Sep 29, 2020
I've created a doc that lays out potential future investments in Pigeon to help the company understand the long term vision. Even though we are in the middle of implementing async callbacks, it's on the list. You might be interested in the doc and have some feedback:
https://docs.google.com/document/d/1R-jjZnYUIwpG7Y5EkoHODmlvVeN1zz2Y-lCjTOku9Y0/edit#
cc @xsahil03x
fzyzcjy commentedon Sep 30, 2020
@gaaclarke 🎉 🎉 🎉 The merge PR looks wonderful! And the features in doc also looks appealing!
nslogx commentedon Nov 12, 2020
@gaaclarke Any updates for @hostapi? Thanks!
vexorsis commentedon Dec 10, 2020
any updates?
caikaidev commentedon Dec 10, 2020
@gaaclak
This mr only work for android platfrom . The synchronization code is still generated on iOS. Thanks!
gaaclarke commentedon Feb 17, 2021
Implemented for ios in flutter/packages#282
github-actions commentedon Aug 5, 2021
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.