Use function return value in string in dart

247 Views Asked by At

I want to use the return value of a function, which is a string, with a string inside a Text Widget. This is my code, task is an object of a class. It contains the date attribute.

Text(
   'Due to $getCurrentDate(task.date)',
    style: TextStyle(
    fontWeight: FontWeight.w200,
    color: Colors.grey,
    ),

Used function

String getCurrentDate(DateTime date) {
  var currentDate = date.toString();

  var dateParse = DateTime.parse(currentDate);

  var formattedDate = "${dateParse.day}.${dateParse.month}.${dateParse.year}";

  return formattedDate;
}

When I use the code like this, which is what I want to achieve, it displays "Due to Closure:(DateTime) => String from Function 'getCurrentDate':static. (task.date)",

When I use a Text Widget like this, the date is displayed properly. e.g this is displayed "11.2.2023"

Text(
   getCurrentDate(task.date),
    style: TextStyle(
    fontWeight: FontWeight.w200,
    color: Colors.grey,
    ),

But I want to achieve that it displays "Due to 11.2.2023"

1

There are 1 best solutions below

0
comhendrik On

Use

'Due to ${getCurrentDate(task.date)}'

instead of

'Due to $getCurrentDate(task.date)'