I am using Firestore as a database in my Flutter app, and I am using a StreamBuilder to access the data. But, whenever I try to do a delete operation using a button press, Flutter always returns this error:
The following StateError was thrown building StreamBuilder<DocumentSnapshot<Object?>>(dirty, state:
_StreamBuilderBaseState<DocumentSnapshot<Object?>, AsyncSnapshot<DocumentSnapshot<Object?>>>#3bac1):
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist
but then proceeds to delete the item. I am able to add, update and read all the items in the collection, but this error always appears whenever I try to delete them. How could I make the error stop appearing?
This is the widget where the error occurs (It's quite long, I know), but I have removed some parts because they weren't very applicable or useful in this issue.
StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
stream: FirebaseFirestore.instance
.collection('users')
.doc(widget.uid)
.collection('passwords')
.doc(widget.password['appName'])
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const SizedBox(),
Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 0),
child: SizedBox.fromSize(
size: const Size.fromHeight(80),
child: Container(
decoration: BoxDecoration(
color: const Color(0xFF0C163F),
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(16, 14, 0, 0),
child: Text(
'Login:',
style: TextStyle(color: Colors.white),
textAlign: TextAlign.left,
),
),
Padding(
padding:
const EdgeInsets.fromLTRB(18, 4, 0, 0),
child: Text(
snapshot.data!['login'],
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
],
),
const Spacer(),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: GestureDetector(
child: const Icon(
Icons.copy,
color: Colors.white,
),
onTap: () {
Clipboard.setData(ClipboardData(
text:
snapshot.data!['login'],));
},
),
)
],
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12, 12, 12, 0),
child: SizedBox.fromSize(
size: const Size.fromHeight(80),
child: Container(
decoration: BoxDecoration(
color: const Color(0xFF0C163F),
borderRadius: BorderRadius.circular(15)),
child: Row(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Padding(
padding: EdgeInsets.fromLTRB(16, 14, 0, 0),
child: Text('Password:',
style: TextStyle(color: Colors.white)),
),
FittedBox(
fit: BoxFit.cover,
child: Padding(
padding: const EdgeInsets.fromLTRB(
18, 4, 0, 0),
child: Text(
snapshot.data![‘password’]
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
),
)
],
),
const Spacer(),
Padding(
padding: const EdgeInsets.only(right: 16.0),
child: GestureDetector(
child: const Icon(
Icons.remove_red_eye,
color: Colors.white,
),
onTap: () async {
},
),
),
Padding(
padding: const EdgeInsets.only(right: 12.0),
child: GestureDetector(
child: const Icon(
Icons.copy,
color: Colors.white,
),
onTap: () {
Clipboard.setData(ClipboardData(
text:
snapshot.data!['password']
));
},
),
)
],
),
),
),
),
Padding(
padding: const EdgeInsets.fromLTRB(20, 12, 20, 0),
child: Row(
children: const [
Text('URLs:',
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 15)),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(5, 12, 5, 0),
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: snapshot.data!['url'].length,
itemBuilder: (context, index) {
List datalist = snapshot.data!['url'];
return Card(
child: ListTile(
title: Text(
datalist[index],
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 18),
),
));
}),
)
,
const Spacer(),
Padding(
padding: const EdgeInsets.fromLTRB(12, 8, 12, 22),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
minimumSize: const Size.fromHeight(50),
primary: const Color.fromARGB(255, 150, 13, 13),
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(10))),
),
child: const Text('Delete Password'),
onPressed: () async {
await FirebaseFirestore.instance
.collection('users')
.doc(widget.uid)
.collection('passwords')
.doc(widget.password(appname))
.delete();
},
))
],
),
);
}
}),
);
}
}
This is the Firebase Console view: