tStringOrNull function

String? tStringOrNull(
  1. dynamic value, {
  2. bool allowEmpty = false,
})

Implementation

String? tStringOrNull(dynamic value, {bool allowEmpty = false}) {
  String? targetValue;
  try {
    if (value == null) {
      targetValue = null;
    } else if (value is String) {
      targetValue = value;
    } else if (value is Map || value is List) {
      targetValue = jsonEncode(value);
    } else if (value is int || value is double) {
      return value.toString();
    } else if (value is bool) {
      targetValue = value.toString();
    } else {
      targetValue = value.toString();
    }
  } catch (e) {
    targetValue = null;
  }

  if (!allowEmpty && targetValue != null && targetValue.trim().isEmpty) {
    return null;
  }
  return targetValue;
}