getByPath<R> method

R getByPath<R>(
  1. List path, {
  2. dynamic def,
})

Implementation

R getByPath<R>(List<dynamic> path, {dynamic def}) {
  try {
    var key = path.removeAt(0);
    if (path.isEmpty) {
      return get<R>(key, def: def);
    } else {
      if (path[0] is int) {
        var index = path.removeAt(0) as int;
        var valueKey = get<dynamic>(key);
        var valueIndex = valueKey.get<dynamic>(index);

        if ((valueIndex is ModelLess || valueIndex is ModelLessArray) &&
            path.isNotEmpty) {
          return valueIndex.getByPath<dynamic>(path, def: def);
        }
        return valueIndex as R;
      }
      return get<ModelLess>(key).getByPath<R>(path, def: def);
    }
  } catch (e) {
    if (T == String && def == null) return "" as R;
    return def as R;
  }
}