Flutter: type 'String' is not a subtype of type 'DateTime'
Asked Answered
D

1

12

I want to get TimeStamp from mysql & Change it into time ago My TimeStamp is look like:

2021-03-04 06:06:48

And in Flutter:

[{id: 1, caption: first post, useruid: b0zbRHZEKMbGSdNoVSjI0gsxXTX2, time: 2021-03-04 06:06:48,}],

Error type 'String' is not a subtype of type 'DateTime'

Here i call to change my date & time to timeago with the help of this plugin..

ListView(
        children: snapshot.data.map<Widget>((document) {
      Provider.of<PostFunctions>(context, listen: false)
          .showTimeAgo(document['time']);
return My Other Code...
)


I show like this in My Text
' , ${Provider.of<PostFunctions>(context, listen: false).getImageTimePosted.toString()}'

I Change my Date&Time Here

  showTimeAgo(dynamic timedata) {
    print(timedata);

    imageTimePosted = timeago.format(timedata);
    print(' my time posted ***** $imageTimePosted');
    notifyListeners();
  }

enter image description here

Deaconry answered 5/3, 2021 at 11:56 Comment(0)
W
21

You probably need to use DateTime.tryParse() method to convert your String into a DateTime since the package use a DateTime and you're giving a String by doing as follow:

showTimeAgo(document['time']);

Make this change:

showTimeAgo(DateTime.tryParse(document['time']));
Wobbling answered 5/3, 2021 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.