toStringJson static method

String? toStringJson(
  1. dynamic json, {
  2. bool trim = true,
  3. bool makeUppercase = false,
  4. bool makeLowercase = false,
  5. bool makeCapitalized = false,
})

Converts JSON to a string with optional transformations.

Implementation

static String? toStringJson(
  dynamic json, {
  bool trim = true,
  bool makeUppercase = false,
  bool makeLowercase = false,
  bool makeCapitalized = false,
}) {
  if (json == null) return null;
  String? result = json is String ? json : json.toString();
  if (trim) result = result.trim();
  if (makeUppercase) {
    result = result.toUpperCase();
  } else if (makeLowercase) {
    result = result.toLowerCase();
  } else if (makeCapitalized) {
    result = result
        .split(' ')
        .map((String w) {
          if (w.isEmpty) return w;
          return w[0].toUpperCase() + w.substringSafe(1).toLowerCase();
        })
        .join(' ');
  }
  return result.isEmpty ? null : result;
}