trackCall< T> static method
Reports that a method call has started.
Only StandardMessageCodec types are accepted as methodArgs
:
null, bools, nums, Strings, Uint8Lists, Int32Lists, Int64Lists,
Float64Lists, Lists of supported values, Maps from supported values to
supported values.
Method might throw Exception.
void main() async {
final myURL = "http://www.mysite.com/news";
final news = await Instrumentation.trackCall(
className: "MainScreen",
methodName: "getNews",
methodArgs: [myURL],
methodBody: () async {
final result = await get(myURL);
return result;
});
}
Implementation
static Future<T?> trackCall<T>({
required String className,
required String methodName,
required FutureOr<T> Function() methodBody,
dynamic methodArgs,
String? uniqueCallId,
}) async {
final callId = uniqueCallId ?? UniqueKey().toString();
final args = {
"callId": callId,
"className": className,
"methodName": methodName,
"methodArgs": methodArgs
};
try {
await channel.invokeMethod<void>('beginCall', args);
} on PlatformException catch (e) {
throw Exception(e.details);
}
FutureOr<T> onSuccess(dynamic result) async {
final args = {"callId": callId, "result": result};
await channel.invokeMethod<void>('endCallWithSuccess', args);
return result;
}
Future<T?> onError(dynamic e) async {
var error = <String, String>{};
if (e is Error) {
error["stackTrace"] = e.stackTrace.toString();
error["message"] = "Error";
} else {
error["message"] = e.toString();
}
final args = {"callId": callId, "error": error};
await channel.invokeMethod<void>('endCallWithError', args);
return null;
}
try {
final callback = methodBody();
if (callback is Future<T>) {
final res = await callback;
return onSuccess(res);
} else {
return onSuccess(callback);
}
} catch (e) {
return onError(e);
}
}