hasProperty static method

bool hasProperty(
  1. dynamic obj,
  2. String? name
)

Checks if object has a property with specified name.

The object can be a user defined object, map or array. The property name correspondently must be object property, map key or array index.

  • obj an object to introspect.
  • name a name of the property to check. Returns true if the object has the property and false if it doesn't.

Implementation

static bool hasProperty(obj, String? name) {
  if (obj == null || name == null) {
    return false;
  }

  if (obj is IValueWrapper) obj = obj.innerValue();

  if (obj is Map) {
    var targetKey = name.toLowerCase();
    for (var key in obj.keys) {
      if (key.toString().toString() == targetKey) return true;
    }
  } else if (obj is List) {
    var index = IntegerConverter.toNullableInteger(name);
    return index != null && index < obj.length;
  } else {
    return PropertyReflector.hasProperty(obj, name);
  }

  return false;
}