Issue
I’m trying to make a favorite button in my flutter app with Firebase. But when I use snapshot.hasData to see if the particular item is already present in favorite list, it always returns true, even if the item is not present in the database. So I tried snapshot.data.exists and it works. But, eventhough the app is working fine", it always shows following error in the debug console:
The getter 'exists' was called on null.
Receiver: null
Tried calling: exists
My Full code:
Widget build(BuildContext context) {
return StreamBuilder<DocumentSnapshot>(
stream: FirebaseFirestore.instance
.collection("UserData")
.doc(_auth.currentUser.uid)
.collection("Favourites")
.doc(widget.items["name"])
.snapshots(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
return Scaffold(
body: Row(
children: [
snapshot.data.exists
? Expanded(
child: TextButton.icon(
onPressed: () {
FirebaseFirestore.instance
.collection("UserData")
.doc(_auth.currentUser.uid)
.collection("Favourites")
.doc(widget.items["name"])
.delete();
},
label: Text(
"Unfavourite Item",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).accentColor),
),
icon: Icon(
Icons.star,
color: Theme.of(context).accentColor,
),
style: TextButton.styleFrom(
minimumSize: Size.fromHeight(50),
elevation: 0),
),
)
: Expanded(
child: TextButton.icon(
onPressed: () {
FirebaseFirestore.instance
.collection("UserData")
.doc(_auth.currentUser.uid)
.collection("Favourites")
.doc(widget.items["name"])
.set({
"name": widget.items["name"],
"image": widget.items["image"],
"price": widget.items["price"],
"locate": widget.items["locate"],
"assorted": true
});
},
label: Text(
"Favourite Item",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).accentColor),
),
icon: Icon(
Icons.star_border,
color: Theme.of(context).accentColor,
),
style: TextButton.styleFrom(
minimumSize: Size.fromHeight(50),
elevation: 0),
)),
],
),
);
}
);
}
Please help. I’m new to flutter and firebase.
Solution
Your snapshot is null at that time. So handle it like
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.done){
return Scaffold(
body: Row(
children: [
snapshot.data.exists
? Expanded(
child: TextButton.icon(
onPressed: () {
FirebaseFirestore.instance
.collection("UserData")
.doc(_auth.currentUser.uid)
.collection("Favourites")
.doc(widget.items["name"])
.delete();
},
label: Text(
"Unfavourite Item",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).accentColor),
),
icon: Icon(
Icons.star,
color: Theme.of(context).accentColor,
),
style: TextButton.styleFrom(
minimumSize: Size.fromHeight(50), elevation: 0),
),
)
: Expanded(
child: TextButton.icon(
onPressed: () {
FirebaseFirestore.instance
.collection("UserData")
.doc(_auth.currentUser.uid)
.collection("Favourites")
.doc(widget.items["name"])
.set({
"name": widget.items["name"],
"image": widget.items["image"],
"price": widget.items["price"],
"locate": widget.items["locate"],
"assorted": true
});
},
label: Text(
"Favourite Item",
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).accentColor),
),
icon: Icon(
Icons.star_border,
color: Theme.of(context).accentColor,
),
style: TextButton.styleFrom(
minimumSize: Size.fromHeight(50), elevation: 0),
)),
],
),
);
}else{
return SizedBox(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: const Center(
child: CircularProgressIndicator(),
),
);
}
}
Answered By – Nikhil Badyal
Answer Checked By – Pedro (FlutterFixes Volunteer)