objcInstanceFromPointer function

dynamic objcInstanceFromPointer(
  1. Pointer<Void> arg,
  2. String? type
)

Convert arg to its custom type, which is annotated with @native().

Implementation

dynamic objcInstanceFromPointer(Pointer<Void> arg, String? type) {
  if (arg == nullptr) {
    return arg;
  }
  // delete '?' for null-safety
  if (type != null) {
    if (type.endsWith('?')) {
      type = type.substring(0, type.length - 1);
    }
    // check if type is collection, ignore the type of its elements.
    for (String keyword in ['List', 'Map', 'Set']) {
      if (type?.startsWith(keyword) ?? false) {
        type = keyword;
        break;
      }
    }
  } else {
    /// Retrive class name from native.
    var object = NSObject.fromPointer(arg);
    type = object.isa?.name;
  }
  // Create instance of type using converter functions.
  if (type != null) {
    ConvertorFromPointer? convertor = convertorForType(type);
    if (convertor != null) {
      return convertor(arg);
    }
  }
  return NSObject.fromPointer(arg);
}