callbackAsFuture method

Future callbackAsFuture(
  1. Function func, [
  2. dynamic arg0,
  3. dynamic arg1,
  4. dynamic arg2,
  5. dynamic arg3,
  6. dynamic arg4,
  7. dynamic arg5,
])
inherited

Implementation

Future<dynamic> callbackAsFuture(Function func,
    [arg0, arg1, arg2, arg3, arg4, arg5]) async {
  Completer completer = new Completer();
  try {
    String arguments = func.runtimeType.toString().split(' => ')[0];
    var result;
    if (arguments.length > 3) {
      String args = arguments.substring(1, arguments.length - 1);
      args = args.replaceAll(RegExp("<(.*)>"), "");
      int argc = args.split(', ').length;
      switch (argc) {
        case 1:
          result = await func(arg0 ?? null);
          break;
        case 2:
          result = await func(arg0 ?? null, arg1 ?? null);
          break;
        case 3:
          result = await func(arg0 ?? null, arg1 ?? null, arg2 ?? null);
          break;
        case 4:
          result = await func(
              arg0 ?? null, arg1 ?? null, arg2 ?? null, arg3 ?? null);
          break;
        case 5:
          result = await func(arg0 ?? null, arg1 ?? null, arg2 ?? null,
              arg3 ?? null, arg4 ?? null);
          break;
        case 5:
          result = await func(arg0 ?? null, arg1 ?? null, arg2 ?? null,
              arg3 ?? null, arg4 ?? null, arg5 ?? null);
          break;
      }
    } else {
      result = await func();
    }
    completer.complete(result);
  } catch (error) {
    completer.completeError(error);
  }
  return completer;
}