flattenQueryEntry function

void flattenQueryEntry({
  1. required Map<String, dynamic> output,
  2. required String key,
  3. required dynamic value,
})

Implementation

void flattenQueryEntry({
  required Map<String, dynamic> output,
  required String key,
  required dynamic value,
}) {
  final normalizedValue = normalizeValue(value);

  if (shouldSkipValue(normalizedValue)) return;

  if (normalizedValue is Map) {
    for (final entry in normalizedValue.entries) {
      final nestedKey = entry.key?.toString();

      if (nestedKey == null || nestedKey.trim().isEmpty) continue;

      flattenQueryEntry(
        output: output,
        key: '$key.$nestedKey',
        value: entry.value,
      );
    }

    return;
  }

  if (normalizedValue is Iterable && normalizedValue is! String) {
    final items = normalizedValue
        .map(normalizeValue)
        .where((item) => !shouldSkipValue(item))
        .map((item) => item.toString())
        .toList();

    if (items.isEmpty) return;

    // CSV style:
    // fields=id,name,status
    // sort=sortOrder,-createdAt
    //
    // If your backend wants repeated keys instead:
    // output[key] = items;
    output[key] = items.join(',');
    return;
  }

  output[key] = normalizedValue;
}