Home > Mobile >  Fetching data from Firebase into listview in flutter
Fetching data from Firebase into listview in flutter

Time:08-22

I'm trying to fetch data from firebase and I want that data to be printed in my listview.builder.here is my code

 StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection("passwordProfile")
                .snapshots(),
            builder: (context, snapshot) {
              if (!snapshot.hasData) {
                return CircularProgressIndicator();
              }
              if (snapshot.data.documents.lenght == 0) {
                return Text("no data");
              }
              return ListView.builder(
                  itemCount: snapshot.data.documents.lenght,
                  itemBuilder: (context, index) {
                    return ListTile(
                      leading: CircleAvatar(
                          child:
                              snapshot.data.documents[index].data("url")),
                      title: snapshot.data.documents[index].data["url"],
                    );
                  });
            }),

and below is a picture

picture

from the picture you can observe that documents is not being recognized so help there

CodePudding user response:

Once you check null, you can add ! like snapshot.data!

builder: (context, snapshot) {
  if (!snapshot.hasData) {
    return CircularProgressIndicator();
  }
  final data = snapshot.data as Map<String, dynamic>?;
  if (data == null) {
    return Text("no data");
  }
  return ListView.builder(
      itemCount: data.length,
      itemBuilder: (context, index) {
        return ListTile(
          // leading:
          //     CircleAvatar(child: data[index].data("url")),
          title: Text("${data[index].data["url"]}"),
        );
      });
}),

CodePudding user response:

Please specify SteamBuilder return type i-e Querysnapshot here's the example of your code

StreamBuilder<QuerySnapshot>(
                stream: FirebaseFirestore.instance
                    .collection("passwordProfile")
                    .snapshots(),
                builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return const CircularProgressIndicator();
                  }
                  final userSnapshot = snapshot.data?.docs;
                  if (userSnapshot!.isEmpty) {
                    return const Text("no data");
                  }
                  return ListView.builder(
                      itemCount: userSnapshot.length,
                      itemBuilder: (context, index) {
                        return ListTile(
                          leading: CircleAvatar(
                            child: userSnapshot[index]["url"],
                          ),
                          title: userSnapshot[index]["url"],
                        );
                      });
                });
  • Related