skip to Main Content

ListView populated from snapshot values, values are inside a Text.

The data is a timestamp in Firestore looking like this:

Image

Currently they are generated like:

Image

Added toDate()

Text(
      'Date: ${snapshot.data!.docs[index].get('usoOn').toDate()}',
      style: Theme.of(context).textTheme.headline4,
                        ),

and used to look like this:

Image

Had to remove toDate() because of an error NoSuchMethodError timestamp has no instance method to date. Similar to the one below.

Trying to change the format to look like this, it works with errores, both in the console and if you scroll below the ListView you get a big red message.

enter image description here

Most of the answers around that I found are related to using a String or a single value, not a populated ListView taking data from a snapshot.

Thank you for any help with this, still green on Flutter.

2

Answers


  1. Chosen as BEST ANSWER

    Changed to use FirestoreListView, it has better way to change the data type. Also some of the errors related to class int are because some of the data was wrong. Some was manually added during testing and had numerical instead of timestamp.


  2. you can use this extension, add it some global scope (outside your class) :

    extension DateTimeExt on DateTime {
      String yourFormat() {
        final date = this;
        final hour = date.hour.toString().padLeft(2, "0");
        final minute = date.minute.toString().padLeft(2, "0");;
        final day = date.day.toString().padLeft(2, "0");;
        final month = date.month.toString().padLeft(2, "0");;
        final year = date.year.toString().substring(2);
        return "$hour:$minute - $day-$month-$year";
      }
    }
    

    and use it like this example:

      DateTime date = DateTime.now();
      print(date.yourFormat()); // 03:05 - 30-11-2022
    

    in your example you can use it like this:

    Text(
          'Date: ${snapshot.data!.docs[index].get('usoOn').toDate().yourFormat()}',
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search