This is what I wanted as on UI.
This is my formatter
static String formatMonthToYear(DateTime? date, BuildContext context){
if (date == null) return "Invalid date";
var formatter = DateFormat('yMMMM');
return formatter.format(date);
}
I want to put date time here in Text.
Text(DateTimeUtil.formatMonthToYear(invoiceTotal.data!.map((e) => e.dueDate) as DateTime, context),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'quicksand_bold',
fontSize: 20
),
),
This is the data i'm passing.
dueDate: json["due_date"] == null? null: DateTime.parse(json["due_date"]),
CodePudding user response:
First of all
static String formatMonthToYear(DateTime? date){ // <=== change here because you do not need to pass context.
if (date == null) return "Invalid date";
var formatter = DateFormat('yMMMM');
return formatter.format(date);
}
Then try this...
Text(DateTimeUtil.formatMonthToYear(invoiceTotal.data!.map((e) => DateTime.parse(e.dueDate).toString())),// <=== change here
style: const TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'quicksand_bold',
fontSize: 20
),
),
because when you are passing Date it is in String so rather then assign as DateTime above is better way to do.