Ad

Type 'DateTime' Is Not A Subtype Of Type 'String' In Flutter Cloud Firestore

I'm trying to get DateTime.now() and write/read it with Cloud FireStore. But got the error

This is my code

In the UI

Text( 
DateFormat('dd/MM/yyyy kk:mm')
.format(DateTime
.fromMillisecondsSinceEpoch(int.parse(document[index].data['timeCreated']))),
style: timeStyle,
),

Writing data to Firestore

Firestore.instance.runTransaction((transaction) async {
      await transaction.set(
        docRef,
        {'timeCreated': DateTime.now().millisecondsSinceEpoch.toString()},
      );
    });

And got this error

I/flutter ( 3378): Another exception was thrown: type 'DateTime' is not a subtype of type 'String'
Ad

Answer

Convert to string:

int time = DateTime.now().millisecondsSinceEpoch;
String t = "$time";
Ad
source: stackoverflow.com
Ad