toMapList static method

List<Map<String, dynamic>>? toMapList(
  1. String? jsonStr
)

JSON 字符串转为 List

⚠️ 精度限制:同 fromJson——大于 2^53 的整数会丢失精度。

Implementation

static List<Map<String, dynamic>>? toMapList(String? jsonStr) {
  if (jsonStr == null || jsonStr.isEmpty) return null;
  try {
    final decoded = json.decode(jsonStr);
    if (decoded is! List) return null;

    return decoded
        .map((e) {
          if (e is Map<String, dynamic>) {
            return e;
          } else if (e is String) {
            final innerDecoded = json.decode(e);
            return innerDecoded is Map<String, dynamic> ? innerDecoded : null;
          }
          return null;
        })
        .whereType<Map<String, dynamic>>()
        .toList();
  } catch (e) {
    logger.e('JSON 转 Map 列表失败: $e');
    return null;
  }
}