How to display date and time in text widget of flutter?

522

the toDate() method is available in the string_validator package. I think you should use the default DateTime.parse() and use it as:

var parsedDate = DateTime.parse(data['createdAt']);

Then run it through the DateFormat as:

DateFormat('dd/MM/yyyy').format(parsedDate))
Share:
522
Tushar Rai
Author by

Tushar Rai

Updated on November 26, 2022

Comments

  • Tushar Rai
    Tushar Rai over 1 year

    I am trying to display date and time in text widget of flutter with the help of import 'package:intl/intl.dart'; package, but its throwing and error.

    The following NoSuchMethodError was thrown building:

    The method 'toDate' was called on null. Receiver: null Tried calling: toDate()

    Row(
     mainAxisAlignment: MainAxisAlignment.spaceEvenly,
     children: [
        Text('Order Date: ${DateFormat('dd/MM/yyyy').format(data['createdAt'].toDate())}'),
        Text('Order Time: ${DateFormat('hh:mm a').format(data['createdAt'].toDate())}'),
    ],),
    
    • lava
      lava about 2 years
      could you provide your time and date
    • markhorrocks
      markhorrocks about 2 years
      He doesn't have a date, it's null. I think the question is how to display a null date?
  • Tushar Rai
    Tushar Rai about 2 years
    I have tried it already with this. it is throwing an error type 'Timestamp' is not a subtype of type 'String'