getProperties static method

Map<String, dynamic> getProperties(
  1. dynamic obj
)

Get values of all properties in specified object and returns them as a map.

  • obj an object to get properties from. Returns a map, containing the names of the object's properties and their values.

Implementation

static Map<String, dynamic> getProperties(obj) {
  var map = <String, dynamic>{};

  var cm = reflectClass(obj.runtimeType);
  var im = reflect(obj);
  for (var dm in cm.declarations.values) {
    Symbol? foundName;

    if (dm is VariableMirror && !dm.isStatic && !dm.isPrivate) {
      foundName = dm.simpleName;
    } else if (dm is MethodMirror &&
        dm.isGetter &&
        !dm.isStatic &&
        !dm.isPrivate) foundName = dm.simpleName;

    if (foundName != null) {
      try {
        var name = _extractName(foundName);
        var value = im.getField(foundName).reflectee;
        map[name] = value;
      } catch (ex) {
        // Ignore exceptions...
      }
    }
  }

  return map;
}