asLists<T> method Null safety

List<T>? asLists<T>(
  1. List<String> keys,
  2. [dynamic toBean(
    1. Map json
    )?]
)

多字段解析成Lists

Implementation

List<T>? asLists<T>(List<String> keys, [Function(Map json)? toBean]) {
  if (this == null) return null;
  for (String key in keys) {
    try {
      Object? obj = this![key];
      if (obj != null) {
        if (toBean != null) {
          if (obj is List) {
            return obj.map((v) => toBean(v)).toList().cast<T>();
          } else if (obj is String) {
            List _list = jsonDecode(obj);
            return _list.map((v) => toBean(v)).toList().cast<T>();
          }
        } else {
          if (obj is List) {
            return List<T>.from(obj);
          } else if (obj is String) {
            List _list = jsonDecode(obj);
            return List<T>.from(_list);
          }
        }
      }
    } catch (e) {
      print(e);
      _print('json parse failed,exception value::\"$key\":${this![key]}');
      _printDetail('asLists', key, this);
    }
  }

  return null;
}