Home > Back-end >  how to pass a closure as a parameter in method channel's invokeMethod in Flutter plugin
how to pass a closure as a parameter in method channel's invokeMethod in Flutter plugin

Time:07-10

In my native implementation, I have a method that takes two string parameters and a closure. It is a login function. So what it does is to use the user's id and password to decide if the user is allowed to log in. The closure takes a boolean parameter that means the login is either success or failure. Based on the boolean, the contents of the closure takes corresponding actions. In order to make this method available to plugin users, I created this in the dart part:

Future<void>login(String username, String password, void Function(bool isSuccess) callback) async {
   return await methodChannel.invokeMethod('login', 
         {'username': username, 'password': password, 'callback': callback}
   );
}

But the above code caused an error: Exception has occurred. ArgumentError (Invalid argument: Closure: (bool) => void).

It didn't even reach the native code. This looks like that passing closure or function is not allowed?

CodePudding user response:

Correct, functions are not supported. See Platform channel data types support for details.

  • Related