Home > Back-end >  Flutter, display text if firebase data is empty
Flutter, display text if firebase data is empty

Time:05-30

I am fetching data from firebase and displaying it on screen, initially, firebase data will be empty so, I wanna display the textPress to add data. I tried doing it using if conditionsnapshot.data!.docs ==null but didn't work but it didn't work. Once the users add data to firebase, the stream builder will fetch it and display it and the text won't be there. How to achieve that.

StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection('lender')
                        .doc(auth.currentUser!.email)
                        .collection('borrowers')
                        .snapshots(),
                    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData) {
                        return const Center(
                            child: CircularProgressIndicator(
                          backgroundColor: Color(0xff8eacbb),
                        ));
                      }
                      if (snapshot.data!.docs ==null) {
                        return Text(
                          'Press   to add data',
                          style: TextStyle(fontSize: 20),
                        );
                      } else {
                        return ListView(
                            children: snapshot.data!.docs.map((data) {
                          return BorrowerCard(BorrowerName: data['Name']);
                        }).toList());
                      }
                    },
                  )

I also tried displaying the text instead of the CircularProgressIndicator but the text was displayed only for a few seconds, but I want it to display until the user adds data.

CodePudding user response:

Try with null check in the return value section

StreamBuilder(
                    stream: FirebaseFirestore.instance
                        .collection('lender')
                        .doc(auth.currentUser!.email)
                        .collection('borrowers')
                        .snapshots(),
                    builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (!snapshot.hasData) {
                        return const Center(
                            child: CircularProgressIndicator(
                          backgroundColor: Color(0xff8eacbb),
                        ));
                      }else{
                       return snapshot.data!=null?ListView(
                            children: snapshot.data!.docs.map((data) {
                          return BorrowerCard(BorrowerName: data['Name']);
                        }).toList()): Text(
                          'Press   to add data',
                          style: TextStyle(fontSize: 20),
                        );
                      }
                    },
                  )

CodePudding user response:

For a query without results you'll get a snapshot with an empty array of documents. So to check for that:

if (snapshot.data!.docs.isEmpty) {
  • Related