dynamicToMap<T> static method

dynamic dynamicToMap<T>(
  1. dynamic map
)

将动态类型转换成指定类型的 Map 集合,转换前请判断数据类型是否是 Map

Implementation

static dynamic dynamicToMap<T>(dynamic map) {
  assert(map is Map);
  final valueType = T.toString().getMapGenericType;
  String targetKeyType = valueType!.key;
  String targetValueType = valueType.value;
  if (targetKeyType == 'dynamic') {
    if (targetValueType == 'Object') {
      return Map<Object, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<Object, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<Object, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<Object, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<Object, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<Object, bool>.from(map);
    }
    return map.cast<Object, dynamic>();
  }
  if (targetKeyType == 'String') {
    if (targetValueType == 'Object') {
      return Map<String, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<String, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<String, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<String, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<String, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<String, bool>.from(map);
    }
    return Map<String, dynamic>.from(map);
  }
  if (targetKeyType == 'int') {
    if (targetValueType == 'Object') {
      return Map<int, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<int, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<int, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<int, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<int, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<int, bool>.from(map);
    }
    return Map<int, dynamic>.from(map);
  }

  if (targetKeyType == 'double') {
    if (targetValueType == 'Object') {
      return Map<double, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<double, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<double, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<double, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<double, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<double, bool>.from(map);
    }
    return Map<double, dynamic>.from(map);
  }
  if (targetKeyType == 'bool') {
    if (targetValueType == 'Object') {
      return Map<bool, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<bool, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<bool, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<bool, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<bool, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<bool, bool>.from(map);
    }
    return Map<bool, dynamic>.from(map);
  }

  if (targetKeyType == 'num') {
    if (targetValueType == 'Object') {
      return Map<num, Object>.from(map);
    }
    if (targetValueType == 'String') {
      return Map<num, String>.from(map);
    }
    if (targetValueType == 'int') {
      return Map<num, int>.from(map);
    }
    if (targetValueType == 'double') {
      return Map<num, double>.from(map);
    }
    if (targetValueType == 'num') {
      return Map<num, num>.from(map);
    }
    if (targetValueType == 'bool') {
      return Map<num, bool>.from(map);
    }
    return Map<num, dynamic>.from(map);
  }

  return map;
}