asString method

String asString({
  1. String? def,
  2. bool trim = true,
})

Converts the dynamic value to a String. If the value is null or empty after trimming, returns the provided default value or an empty string. def The default value to return if the dynamic value is null or empty. trim Whether to trim whitespace from the string (default is true). Returns the String representation of the dynamic value or the default value.

Implementation

String asString({String? def, bool trim = true}) {
  def ??= '';
  var res = (this ?? def).toString();
  if (trim) {
    res = res.trim();
  }
  return res.isEmpty ? def : res;
}