execExpressionToString method

String? execExpressionToString(
  1. String expressionStr,
  2. String? format
)

Implementation

String? execExpressionToString(String expressionStr, String? format) {
  if (format == '') {
    format = null;
  }
  try {
    var value = execExpressionToValue(expressionStr);
    DateTime? date;
    if (value == null) {
      return null;
    }
    if (value is String) {
      date = DateTime.tryParse(value);
    }
    if (value is num) {
      var numberFormat = new NumberFormat(format ?? '#################.##');
      return numberFormat.format(value);
    } else if (value is DateTime) {
      var dateFormat = new DateFormat(format ?? 'yyyy-MM-dd HH:mm');
      return dateFormat.format(value);
    } else if (date != null) {
      var dateFormat = new DateFormat(format ?? 'yyyy-MM-dd HH:mm');
      return dateFormat.format(date);
    } else if (value is String) {
      if (format == null) {
        return value;
      } else {
        return format.replaceAll('{0}', value);
      }
    } else {
      return format;
    }
  } catch (_) {
    return null;
  }
}