asList<T> method Null safety

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

解析成List

Implementation

List<T>? asList<T>(String key, [Function(Map json)? toBean]) {
  if (this == null) return null;
  try {
    Object? obj = this![key];
    if (toBean != null && obj != 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 != null) {
      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('asList', key, this);
  }
  return null;
}