skip to Main Content

new to flutter and working on this error.
this is the code.

PROBLEM- I want to to show the text(your cart is empty!) when the firestore is empty. The text is displaying when the firestore document is empty but the problem is when firestore has some data to display, the text(your cart is empty!) will display and after a moment(0.5 sec) the data will display.

I don’t want to display the text (your cart is empty) even for a milliseconds when the firestore has data. I appreciate if you guys can light the bulb for me.

2

Answers


  1. With FutureBuilder, the app is listening to changes to the database. What you are noticing is the latency between a change being made, and your app receiving it. This can never be absolute 0 because a reading device only sees a change after the change has been made. You can decrease this number with optic internet (fast internet connection) and by moving the database closer to where you are. There are multiple regions that you can choose from in order to get you as close as possible.

    Login or Signup to reply.
  2. that happened because you didn’t get response yet for the database so to make sure that you received respond you will use snapshot.hasData

        StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('User-Cart-Item')
                .doc(FirebaseAuth.instance.currentUser!.email)
                .collection("items")
                .snapshots(),
            builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasData) {
                if (snapshot.data == null || snapshot.data!.docs.isEmpty) {
                  return const Center(
                    child: Text(
                      'YOUR CART IS EMPTY!',
                      style: TextStyle(
                        color: Colors.purple,
                        fontWeight: FontWeight.bold,
                        fontSize: 20,
                      ),
                    ),
                  );
                } else {
                  return ListView.builder(
                    itemCount:
                        snapshot.data == null ? 0 : snapshot.data!.docs.length,
                    itemBuilder: (_, index) {
                      DocumentSnapshot documentSnapshot =
                          snapshot.data!.docs[index];
                      return Card(
                        color: const Color.fromARGB(255, 255, 248, 250),
                        margin: const EdgeInsets.all(20),
                        child: Container(
                          height: 120,
                          padding: const EdgeInsets.all(0),
                          child: Row(
                            children: [
                              Expanded(
                                flex: 6,
                                child: Container(decoration: const BoxDecoration()),
                              ),
                            ],
                          ),
                        ),
                      );
                    },
                  );
                }
              } else {
                return const Center(child: CircularProgressIndicator());
              }
            },
          ),
    

    I hope this work for you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search