skip to Main Content

i have a streambuilder to display grid of 3 image, but i get an error about datatype in the streambuilder. ‘String’ is not a subtype of type ‘DateTime’, so i use toDate() but the method is not working. before that i get ‘TimeStamp’ is not a subtype of type ‘DateTime’ too, is there an easy way for managing datatype from firestore database?


  final Stream<QuerySnapshot> _constructed = FirebaseFirestore.instance
      .collection('fotoupload')
      .orderBy("createdAt", descending: true)
     .snapshots();

  Widget gridViewWidget(String docId, String img, String name, int downloads,
      DateTime date, String postuid, String userImg, String email) {
    return GridView.count(
      primary: false,
      padding: EdgeInsets.all(6),
      crossAxisSpacing: 1,
      crossAxisCount: 1,
      children: [
        GestureDetector(
          onTap: () {
            //createOwnerDetails
          },
          child: Center(
            child: Image.network(
              img,
              fit: BoxFit.fill,
            ),
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    // return Image == null ? buildSplashScreen() : buildUploadForm();
    return Scaffold(body: StreamBuilder<QuerySnapshot>(
        stream: _constructed,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          } else if (snapshot.connectionState == ConnectionState.active) {
            print(snapshot.connectionState);
            print(snapshot.data!.docs);
            print(snapshot
                .data!.docs.length); // check all the data and connectionstate

            if (snapshot.data!.docs.isNotEmpty) {
              return GridView.builder(
                itemCount: snapshot.data!.docs.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3),
                itemBuilder: (BuildContext context, int index) {
                  return gridViewWidget(
                    snapshot.data!.docs[index].id,
                    snapshot.data!.docs[index]['Image'],
                    snapshot.data!.docs[index]['name'],
                    snapshot.data!.docs[index]['downloads'],
                    snapshot.data!.docs[index]['createdAt'].toDate(),//this is the problem
                    snapshot.data!.docs[index]['postid'],
                    snapshot.data!.docs[index]['userImage'],
                    snapshot.data!.docs[index]['email'],
                  );
                },
              );
            } else {
              return Center(
                child: Text(
                  'There is no tasks',
                  style: TextStyle(fontSize: 20),
                ),
              );
            }
          } else {
            return Center(
              child: Text(
                'Something went wrong',
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
              ),
            );
          }
        },
      ),
    );
  }

Firebase Console
Debug Console

i just want to display the data from firestore, is there something wrong from my streambuilder?

2

Answers


  1. You must parse your string to DateTime first, change your error line to this:

    DateTime.fromMicrosecondsSinceEpoch(snapshot.data!.docs[index]['createdAt'])
    

    And you need to add this package to your project for DateTime parse intl

    Login or Signup to reply.
  2. Solution:

    instead of this:

    snapshot.data!.docs[index]['createdAt'].toDate(),//this is the problem
    

    try this:

    (snapshot.data!.docs[index]['createdAt'] == null) ? null : (snapshot.data!.docs[index]['createdAt'] as Timestamp).toDate()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search