Flutter DateTime formate in Time ago in ListView.builder

Issue

I have PageView.builder Widget, which is fetching data from my server.

PageView.builder(
    itemCount: _newsList.length,
          itemBuilder: (context, index){
            if(_newsList.isNotEmpty){
            return Text(_newsList[index].posted!);
       } else Container("Empty");
}
)

I have imported https://pub.dev/packages/timeago
but can not use it in PageView.builder().

Please help me to sort it out.

Solution

To use, import timeago as:

import 'package:timeago/timeago.dart' as timeago;

To use timeago in widget:

Text(timeago.format(DateTime.fromMillisecondsSinceEpoch(dateTimeMillis))); //or other DateTime object

Answered By – Abhishek Dhiman

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *