The signature is void setMethodCallHandler(Future<dynamic> handler(MethodCall call))
, so we need to provide a function at the Dart end that returns Future<dynamic>
, for example _channel.setMethodCallHandler(myUtilsHandler);
Then implement the handler. This one handles two methods foo
and bar
returning respectively String
and double
.
Future<dynamic> myUtilsHandler(MethodCall methodCall) async {
switch (methodCall.method) {
case 'foo':
return 'some string';
case 'bar':
return 123.0;
default:
throw MissingPluginException('notImplemented');
}
}
At the Java end the return value is passed to the success
method of the Result
callback.
channel.invokeMethod("foo", arguments, new Result() {
@Override
public void success(Object o) {
// this will be called with o = "some string"
}
@Override
public void error(String s, String s1, Object o) {}
@Override
public void notImplemented() {}
});
In Swift, the return value is an Any?
passed to the result
closure. (Not implemented is signaled by the any parameter being the const NSObject
value FlutterMethodNotImplemented
.)
channel.invokeMethod("foo", arguments: args, result: {(r:Any?) -> () in
// this will be called with r = "some string" (or FlutterMethodNotImplemented)
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…