skip to Main Content

I have a field stored within a document in Firestore called ‘lastChecked’ which is of type timestamp. I want to retrieve this data and display it in my list view.builder. I am using this code:

ListTile(
      title: Text('${data.docs[index]['name']}'),
      subtitle: Text(data.docs[index]['lastChecked'].toString()),

in place of toString() I have also tried toDate() but this is not recognised.

With the current code as above, I am returned

Timestamp(seconds=xxxxx, nanoseconds=xxxxx) within my list.

This is the full code segment showing my Stream of QuerySnapshots, Streambuilder, and ListView.builder.

   Stream<QuerySnapshot> bags = FirebaseFirestore.instance
        .collection("bags")
        .where("station", isEqualTo: thisUserBase)
        .snapshots();

// scaffold app bar etc within this space //

 child: StreamBuilder<QuerySnapshot>(
              stream: bags,
              builder: (
                BuildContext context,
                AsyncSnapshot<QuerySnapshot> snapshot,
              ) {
                if (snapshot.hasError) {
                  return Text('Error!');
                }

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return CircularProgressIndicator();
                }

                final data = snapshot.requireData;

                return ListView.builder(
                  itemCount: data.size,
                  itemBuilder: (context, index) {
                    return Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Card(
                        elevation: 5,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: data.docs[index]['isChecked']
                            ? null
                            : ListTile(
                                title: Text('${data.docs[index]['name']}'),
                                subtitle: Text(
                                    data.docs[index]['lastChecked'].toString()),
                                trailing: data.docs[index]['isChecked']
                                    ? greenCircle
                                    : redCircle,
                              ),
                      ),
                    );
                  },
                );
              },
            ),

All help gratefully received!

Thanks

EDIT AFTER RESPONSES – this is my current code that gives me the error:

return ListView.builder(
                  itemCount: data.size,
                  itemBuilder: (context, index) {
                    var timestamp = data.docs[index]['lastChecked'];
                    var date =
                        DateTime.fromMicrosecondsSinceEpoch(timestamp * 1000);
                    String formatted = DateFormat('yyyy-MM-dd').format(date);

                    return Padding(
                      padding: const EdgeInsets.all(5.0),
                      child: Card(
                        elevation: 5,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        child: data.docs[index]['isChecked']
                            ? null
                            : ListTile(
                                title: Text('${data.docs[index]['name']}'),
                                subtitle: Text(formatted),
                                trailing: data.docs[index]['isChecked']
                                    ? greenCircle
                                    : redCircle,
                              ),
                      ),
                    );
                  },
                );
              },
            ),

2

Answers


  1. Try that:

       ListTile(
                                        title: Text('${data.docs[index]['name']}'),
                                        subtitle: Text(
                                           DateTime.fromMillisecondsSinceEpoch(data.docs[index]
    ['lastChecked'] * 1000)),
    
    Login or Signup to reply.
  2. First you need to convert your asdas to DateTime like this:

    DateTime date = (data.docs[index]['lastChecked'] as Timestamp).toDate();
    

    then use the intl package and format it to what ever you want:

    String formatted = DateFormat('yyyy-MM-dd').format(date);
    

    and now show formatted in your list:

    ListTile(
       title: Text('${data.docs[index]['name']}'),
       subtitle: Text(formatted),
       trailing: data.docs[index]['isChecked']
                    ? greenCircle
                    : redCircle,
    )
    

    full example:

    ListView.builder(
        itemCount: data.size,
        itemBuilder: (context, index) {
           DateTime date = (data.docs[index]['lastChecked'] as Timestamp).toDate();
           
           String formatted = DateFormat('yyyy-MM-dd').format(date);
    
           return Padding(
               padding: const EdgeInsets.all(5.0),
               child: Card(
                   elevation: 5,
                   shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                   ),
                   child: data.docs[index]['isChecked']
                        ? null
                        : ListTile(
                              title: Text('${data.docs[index]['name']}'),
                              subtitle: Text(formatted),
                              trailing: data.docs[index]['isChecked']
                                        ? greenCircle
                                        : redCircle,
                            ),
                          ),
                        );
                      },
                    )
    

    the result would be something like 2022/7/21.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search