skip to Main Content

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

2

Answers


  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());
            },
          ).
    
    Login or Signup to reply.
  2. just try this,i resolve your issue with some changes,

        final uid = FirebaseAuth.instance.currentUser!.uid;
        QueryDocumentSnapshot<Map<String, dynamic>>? selectedRide;
        
        StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(
          stream: FirebaseFirestore.instance.collectionGroup('Requests').snapshots(),
          builder: (context, snapshot) {
            if (!snapshot.hasData) {
              return const Center(child: CircularProgressIndicator());
            }
        
            final requests = snapshot.data!.docs;
        
            return ListView.builder(
              itemCount: requests.length,
              itemBuilder: (context, index) {
                final currentRequest = requests[index];
                final currentID = currentRequest.get('UID');
                
                if (currentID == uid) {
                  return Card(
                    child: ListTile(
                      onTap: () {
                        setState(() {
                          selectedRide = currentRequest;
                        });
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => MyRidesPageScreen(selectedRide: selectedRide),
                          ),
                        );
                      },
                      title: Row(
                        children: [
                          SizedBox(
                            width: screenWidth * 0.1,
                            child: Text(
                              currentRequest.get('Departure date'),
                              style: const TextStyle(color: Colors.blue),
                            ),
                          ),
                          SizedBox(width: screenWidth * 0.03),
                          SizedBox(
                            width: screenWidth * 0.27,
                            child: Text(currentRequest.get('Departure city')),
                          ),
                          SizedBox(width: screenWidth * 0.02, child: const Text('-')),
                          SizedBox(
                            width: screenWidth * 0.03,
                          ),
                          SizedBox(
                            width: screenWidth * 0.27,
                            child: Text(currentRequest.get('Arrival city')),
                          ),
                          SizedBox(
                            width: screenWidth * 0.1,
                            child: Text(
                              currentRequest.get('Departure time'),
                              style: const TextStyle(color: Colors.blue),
                            ),
                          ),
                        ],
                      ),
                    ),
                  );
                }
        
                return const SizedBox();
              },
            );
          },
    
    );
    

    basically, I Removed the type parameter from StreamBuilder, extracted the requests list from a snapshot, renamed the variable request to currentRequest, simplified the selectedRide assignment, and removed unnecessary null-checks. try that

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