Wait in Flutter Code for Completion of Async Native Request

448 views
Skip to first unread message

Usman Riaz

unread,
Mar 22, 2021, 2:51:29 PM3/22/21
to Flutter Development (flutter-dev)
I am working on a Flutter app, which needs to call an API function "ActivityCompat.requestPermissions" in Native code (Android/Java) from Flutter. The API native code function is asynchronous i.e. the call to requestPermissions returns immediately and the result of the requestPermissions call is returned later by Android.

How can I wait in Flutter, that when I call the API "requestPermissions" from Flutter via methodChannel, the native code does not return to Flutter immediately, instead wait for the ActivityResult and return the final result to Flutter from Native code. Something like in Flutter

int activityResult = await callNativeRequestPermission(); 
//Now activityResult variable contains the final result

Thanks!

Suzuki Tomohiro

unread,
Mar 22, 2021, 6:13:56 PM3/22/21
to Usman Riaz, Flutter Development (flutter-dev)
Read permission_handler’s implementation


--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/0e55f995-4fd4-42cc-88d7-7f195b627991n%40googlegroups.com.

Usman Riaz

unread,
Mar 22, 2021, 6:41:18 PM3/22/21
to Flutter Development (flutter-dev)
Thanks Suzuki for the response.

Do you know by any chance if the solution (to wait for the native async call to complete) you pointed is specific to plugins only?
 

Suzuki Tomohiro

unread,
Mar 22, 2021, 7:42:30 PM3/22/21
to Usman Riaz, Flutter Development (flutter-dev)
No, it’s common to have Future in return value of methods that use PlatformChannel. I’m giving an example you can learn from.

Usman Riaz

unread,
Mar 22, 2021, 8:40:07 PM3/22/21
to Flutter Development (flutter-dev)
I think you are missing the point. Let me know explain a bit in detail

From Flutter code via methodChannel interface, I invoke an API method "requestPermission" - important thing to note is, "requestPermission" method returns immediately. Since the API function has returned, the control will eventually go back to Flutter code. In the meantime, Android will show a permission dialog box to the user, and based on the selection made by the user, Android will call an overridden function onActivityResult or something like that which will contain the result of the user selection. 

Now, could you please enlighten me, how can I get the result of user selection, after the initial methodChannel invocation has returned? 

Suzuki Tomohiro

unread,
Mar 22, 2021, 9:18:16 PM3/22/21
to Usman Riaz, Flutter Development (flutter-dev)
I think it’s typical use case of Future. A method can return a Future immediately and it becomes resolved when the result is available. Are you able to explain why Future does not fit your case?

Reply all
Reply to author
Forward
0 new messages