Flutter: type 'String' is not a subtype of type 'DateTime'

5,394

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']));
Share:
5,394
Radiant Developers
Author by

Radiant Developers

Updated on December 28, 2022

Comments

  • Radiant Developers
    Radiant Developers over 1 year

    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