0
I have a method that queries books that are favorite in the Firestore database, the book page has a star icon in black color, use isFavorite method to query whether the open book is preferred by the user or not, if it is true it should return and change the color of the icon to yellow but the change does not happen.
The updateFavorite method works perfectly by adding and removing the favorite book from the bank when touching the icon, only the color of it that I can’t configure.
InkWell(
child: Icon(
Icons.star,
size: 30,
color: isFavorite == true ? Colors.yellow
: Colors.black,
),
onTap: (){
model.updateFavorite(model.getUserId(), document.documentID);
},
),
==============
Future<bool> isFavorite() async{
firebaseUser = await _auth.currentUser();
DocumentSnapshot favoritesRef = await Firestore.instance.collection("users")
.document(firebaseUser.uid).get();
if(favoritesRef.data["favorites"].contains(document.documentID)){
print("SIM");
return true;
}
else {
print("NÃO");
return false;
}
}
==============
Future<bool> updateFavorite(Future<DocumentReference> uid, String bookId) async{
firebaseUser = await _auth.currentUser();
DocumentReference favoritesRef = Firestore.instance.collection("users")
.document(firebaseUser.uid);
return Firestore.instance.runTransaction((Transaction tx) async{
DocumentSnapshot postSnapshot = await tx.get(favoritesRef);
if(postSnapshot.exists){
if(!postSnapshot.data["favorites"].contains(bookId)){
await tx.update(favoritesRef, <String, dynamic>{
"favorites": FieldValue.arrayUnion([bookId])
});
// Delete de bookId from Favorites
} else {
await tx.update(favoritesRef, <String, dynamic>{
"favorites": FieldValue.arrayRemove([bookId])
});
}
}
}).then((result){
print(firebaseUser.uid);
print(bookId);
return true;
}).catchError((error){
print("Error: $error");
print("DEU RUIM");
return false;
});
}
Hello you ever tried to put inside a
setState
I’m not seeing state control– Jameson
As @Jameson said, I didn’t see any state control in the code. Try to do a treatment so that after you get the data of the information you will use on your screen, you can make the call some state control method. "Setstate" is the simplest of them. The moment it is called the Flutter will build the component again, thus drawing the correct color.
– Leonardo Paim