-1

I'm having trouble with my code. It's always returning Bad state: field does not exist within the DocumentSnapshotPlatform.

I don't know what I did wrong. It used to work but after sometime and then I started getting this error. I didn't change anything about it. I didn't touch it. All of a sudden started receiving this error. And it doesn't work anymore. Not at all.

final uid = FirebaseAuth.instance.currentUser!.uid;
  QueryDocumentSnapshot<Map<String, dynamic>>? selectedRide;

StreamBuilder(
          stream: FirebaseFirestore.instance
              .collectionGroup('Requests')
              .snapshots(),
          builder: (BuildContext context,
              AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
            if (!snapshot.hasData) {
              return const Center(child: CircularProgressIndicator());
            }

            return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context, index) {
                  final request = snapshot.data!;
                  String currentID = request.docs[index].get('UID');
                  if (currentID == uid) {
                    return Card(
                      child: ListTile(
                        onTap: () {
                          setState(() {
                            selectedRide = snapshot.data!.docs[index];
                          });
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => MyRidesPageScreen(
                                      selectedRide: selectedRide)));
                        },
                        title: Row(
                          children: [
                            SizedBox(
                              width: screenWidth * 0.1,
                              child: Text(
                                request.docs[index].get('Departure date'),
                                style: const TextStyle(color: Colors.blue),
                              ),
                            ),
                            SizedBox(
                              width: screenWidth * 0.03,
                            ),
                            SizedBox(
                              width: screenWidth * 0.27,
                              child: Text(
                                request.docs[index].get('Departure city'),
                              ),
                            ),
                            SizedBox(
                              width: screenWidth * 0.02,
                              child: const Text(
                                '-',
                              ),
                            ),
                            SizedBox(
                              width: screenWidth * 0.03,
                            ),
                            SizedBox(
                              width: screenWidth * 0.27,
                              child: Text(
                                request.docs[index].get('Arrival city'),
                              ),
                            ),
                            SizedBox(
                              width: screenWidth * 0.1,
                              child: Text(
                                request.docs[index].get('Departure time'),
                                style: const TextStyle(color: Colors.blue),
                              ),
                            ),
                          ],
                        ),
                      ),
                    );
                  }
                  return const SizedBox();
                });
          }),

This is the screenshot. Any help?

error screen

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
Eben Oasis
  • 69
  • 6

1 Answers1

1

You are see only one condition !snapshot.hasData but not considering any other condition and trying to build the data using ListView.Builder, but you need to consider situation when the stream is in ConnectionState.waiting and the like

Change your Streambuilder structure to:

StreamBuilder(
        stream: ...,
        builder: (BuildContext context, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.hasData && snapshot.data?.isNotEmpty == true) {
            return ListView.builder(); // Your ListView Builder here
          }
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          }
          return const Center(child: CircularProgressIndicator());
        },
      ).
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88