structToMap function

Map<String, Object?> structToMap(
  1. Map<String, Object?> struct
)

Converts a google.protobuf.Struct JSON map to a plain Dart map.

A Struct has a single "fields" key whose value is a map of string keys to google.protobuf.Value objects. This function recursively converts each Value to its native Dart representation.

If struct already lacks a "fields" wrapper (i.e. it is already a flat map of Values), it is treated as the fields map directly.

Implementation

Map<String, Object?> structToMap(Map<String, Object?> struct) {
  final fields = struct['fields'];
  final Map<String, Object?> fieldMap;

  if (fields is Map<String, Object?>) {
    fieldMap = fields;
  } else {
    // Treat the entire map as the fields map (already unwrapped).
    fieldMap = struct;
  }

  final result = <String, Object?>{};
  for (final entry in fieldMap.entries) {
    if (entry.value is Map<String, Object?>) {
      result[entry.key] = valueToNative(entry.value as Map<String, Object?>);
    } else {
      result[entry.key] = entry.value;
    }
  }
  return result;
}