asList<T> method

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) {
        if (obj.isEmpty) return [];
        //二维数组的处理
        if (obj.is2dArray) {
          //ele.map((v) => toBean(v)).toList(),这一步无法cast,所以会变成List<Dynamic>类型,最终见到的格式就是List<List>
          return obj.map((ele) => ele.map((v) => toBean(v)).toList()).toList().cast<T>();
        }
        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 _listFrom<T>(obj, key);
      } else if (obj is String) {
        return _listFrom<T>(jsonDecode(obj), key);
      }
    }
  } catch (e) {
    print(e);
    _print('json parse failed,exception value::\"$key\":${this![key]}');
    _printDetail('asList', key, this);
  }
  return null;
}