I am creating a bookstore where customers who have their favorite books have a favBook list and the website displays the name and images of the book of all their favorite books. The book model looks something like this:
class BookModel
{
String? bookImage;
String? bookName;
String ? bookId;
BookModel({ this.bookImage, this.bookName,this.bookId});
//data from server
factory BookModel.fromMap(map)
{
return BookModel(
bookImage: map['bookImage'],
bookName: map['bookName'],
bookId: map['bookId'],
);
}
// data to server
Map<String, dynamic> toMap(){
return{
'bookImage': bookImage,
'bookName': bookName,
'bookId':bookId,
};
}
}
For now, I have created a list of strings with the bookId on the main page as well as called my BookModel and have a list of books available stored as such:
List<String> favBooks = ["C8n4Tjwof7RhIspC7Hs","Hc3hTsWkg9vHwGN37Jb"];
List<BookModel> bookList = [];
@override
void initState()
{
super.initState();
FirebaseFirestore.instance
.collection("books")
.where("bookId", isEqualTo: favBooks[0])
.get()
.then((value){
if (value != null && value.docs.isNotEmpty) {
bookList = value.docs.map((doc) => BookModel.fromMap(doc.data())).toList();
setState(() {
});
} else {
}
});
}
and then in my build widget im calling the booklist as such:
final books= ListView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: favBooks.length,
itemBuilder: (BuildContext context,int index)
{
return ListTile(
leading: Ink.image(
image: NetworkImage(bookList[0].bookImage.toString()),
height: 70,
width: 70,
),
title: Text(bookList[0].bookName.toString()),
);
}
);
this is how my database looks like:
[![enter image description here][1]][1]
the issue I'm facing now is that in my where clause when I do favBooks[0] it displays the data but I want to display book the books and now just one book. I tried doing favBooks to be able to get book names and images of the 2 books in the favBook list but it does not work. How do I do it so that when I pass a list of books Id I get the name and image of those lists? (edit: I converted the button to a list view and added image of database)
CodePudding user response:
I am not sure if I understand the question correctly, but if you are asking how to get collection of all books that have ID's of those favorite books (e.g. your favBooks list) i don't think that would be possible by single query like that. My suggestion would be to create additional field in your FS database in your User. (lets say you have collection Users) in this collection where you store users as documents with unique ID's, for each user you create additional field FavBooks, whitch would be a list of refferences to the books. Everytime user marks a book as his favourite, you would add a reference to that book to this filed in a user document. Afterwhitch you can simply create a query to select all books references from this list.
CodePudding user response:
instead of .where("bookId", isEqualTo: favBooks[0]) I used .where("bookId", whereIn: favBooks) and it worked