asNum method Null safety

num asNum(
  1. String key
)

解析成int或者double值

Implementation

num asNum(String key) {
  if (this == null) return 0;
  Object? value = this![key];
  if (value == null) return 0;
  if (value is int) return value;
  if (value is double) return value;
  try {
    if (value is String) {
      if (value.contains('.')) {
        return double.parse(value);
      } else {
        return int.parse(value);
      }
    }
  } catch (e) {
    print(e);
    _print('json parse failed,exception value::\"$key\":$value');
    _printDetail('asNum', key, this);
  }
  return 0;
}