- protected
Registers a service extension method with the given name (full
name "ext.flutter.name"). The given callback is called when the
extension method is called. The callback must return a Future
that either eventually completes to a return value in the form
of a name/value map where the values can all be converted to
JSON using JSON.encode, or fails. In case of failure, the
failure is reported to the remote caller and is dumped to the
logs.
The returned map will be mutated.
Source
@protected
void registerServiceExtension({
@required String name,
@required ServiceExtensionCallback callback
}) {
assert(name != null);
assert(callback != null);
final String methodName = 'ext.flutter.$name';
developer.registerExtension(methodName, (String method, Map<String, String> parameters) async {
assert(method == methodName);
dynamic caughtException;
StackTrace caughtStack;
Map<String, dynamic> result;
try {
result = await callback(parameters);
} catch (exception, stack) {
caughtException = exception;
caughtStack = stack;
}
if (caughtException == null) {
result['type'] = '_extensionType';
result['method'] = method;
return new developer.ServiceExtensionResponse.result(JSON.encode(result));
} else {
FlutterError.reportError(new FlutterErrorDetails(
exception: caughtException,
stack: caughtStack,
context: 'during a service extension callback for "$method"'
));
return new developer.ServiceExtensionResponse.error(
developer.ServiceExtensionResponse.extensionError,
JSON.encode(<String, dynamic>{
'exception': caughtException.toString(),
'stack': caughtStack.toString(),
'method': method
})
);
}
});
}