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?
2
Answers
You are see only one condition
!snapshot.hasData
but not considering any other condition and trying to build thedata
usingListView.Builder
, but you need to consider situation when the stream is inConnectionState.waiting
and the likeChange your
Streambuilder
structure to:just try this,i resolve your issue with some changes,
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