How to use get firebase data to StreamBuilder without Listview

882 views Asked by At

I need to show my firebase data in deference places. But in the same page. In every tutorial I followed they use Listview and I need to know there is another way to get data without using the list view

2

There are 2 answers

0
Dineth Siriwardana On

Ya finally I think I found an answer

  final uid = FirebaseAuth.instance.currentUser!.uid;
  userdata = FirebaseFirestore.instance.collection('/users/$uid/userdata');
  

  return Scaffold(
      body: StreamBuilder(
        stream: userdata.snapshots(),
        builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
          if (snapshot.hasError) {
            return Text('Something went wrong');
          }
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Text("Loading");
          }
          try {
            snapshot.data!.docs.map((DocumentSnapshot document) {
              data = document.data()! as Map<String, dynamic>;
            }).toList();
          } catch (e) {
            print(e.toString());
          }
       },
      ),
    );
0
computer lap On
 Expanded(
          child: StreamBuilder(
            stream:
                FirebaseFirestore.instance.collection("posts").snapshots(),
            builder: (context,
                AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>
                    snapshot) {
              if (snapshot.connectionState == ConnectionState.waiting) {
                return const Center(
                  child: CircularProgressIndicator(),
                );
              }

              if (snapshot.hasData) {
                return ListView.builder(
                  itemCount: snapshot.data!.docs.length,
                  itemBuilder: (context, index) {
                    log(snapshot.data!.docs[index].data().toString());

                    return UserVideos(
                      snap: snapshot.data!.docs[index].data(),
                    );
                  },
                );
              }
              log("${snapshot.stackTrace}stack trace");
              log("${snapshot.error}error");
              return const Text("Something went wrong");
            },
          ),
        ),