Jessen Jie
Jessen Jie

Reputation: 367

Streambuilder is not displaying any data from firestore (Duplicate)

i have a streambuilder to display grid of 3 image, is there something wrong with it? The output says "There is no task". when i print using these print(snapshot.data!.docs); it returns 0 in the debug console and the connection is waiting.


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


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


  @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]['createdAt'].toDate(),
                    snapshot.data!.docs[index]['downloads'],
                    snapshot.data!.docs[index]['name'],
                    snapshot.data!.docs[index]['postid'],
                    snapshot.data!.docs[index]['userImage'],
                  );
                },
              );
            } else {
              return Center(
                child: Text(
                  'There is no tasks',
                  style: TextStyle(fontSize: 20),
                ),
              );
            }
          }
          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?

Upvotes: 1

Views: 69

Answers (1)

Canada2000
Canada2000

Reputation: 1708

You are checking if connectionState is waiting:

if (snapshot.connectionState == ConnectionState.waiting) {

Then, checking if active

else if (snapshot.connectionState == ConnectionState.active)

Then, all the rest goes to else.

New:

Add another condition to check for errors from the beginning:

if (snapshot.hasError) {
          return Text('Something went wrong');
        }

Please note, that this part of your code can never be reached, given how you wrote it:

return Center(
            child: Text(
              'Something went wrong',
              style: TextStyle(fontWeight: FontWeight.bold, fontSize: 30),
            ),

Other Error:

There is a typo in the 'createAt ' field... is there a space at the end or no:

.orderBy("createAt", descending: true)

versus here:

snapshot.data!.docs[index]['createAt '].toDate(),

Upvotes: 2

Related Questions