简体   繁体   English

使用列表 flutter firestore 过滤数据

[英]filter data using list flutter firestore

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.我正在创建一个书店,其中拥有自己喜欢的书的顾客有一个 favBook 列表,该网站显示他们所有喜欢的书的名称和图像。 The book model looks something like this:这本书 model 看起来像这样:

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:现在,我已经在主页上创建了一个带有 bookId 的字符串列表,并调用了我的 BookModel 并存储了一个可用的书籍列表,如下所示:

  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.我现在面临的问题是,在我的 where 子句中,当我执行 favBooks[0] 时,它会显示数据,但我想显示书籍,现在只显示一本书。 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.我尝试执行 favBooks 以获取 favBook 列表中两本书的书名和图像,但它不起作用。 How do I do it so that when I pass a list of books Id I get the name and image of those lists?我该怎么做才能在传递图书 ID 列表时获得这些列表的名称和图像? (edit: I converted the button to a list view and added image of database) (编辑:我将按钮转换为列表视图并添加了数据库图像)

[1]: https://i.stack.imgur.com/APYOL.png

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 (eg your favBooks list) i don't think that would be possible by single query like that.我不确定我是否正确理解了这个问题,但是如果你问的是如何收集所有拥有那些最喜欢的书的 ID 的书(例如你的 favBooks 列表),我认为这样的单一查询是不可能的. My suggestion would be to create additional field in your FS database in your User.我的建议是在用户的 FS 数据库中创建附加字段。 (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, which would be a list of references to the books. (假设您有用户集合)在此集合中,您将用户存储为具有唯一 ID 的文档,为每个用户创建额外的字段 FavBooks,这将是对书籍的引用列表。 Every time user marks a book as his favourite, you would add a reference to that book to this filed in a user document.每次用户将一本书标记为他的最爱时,您都会在用户文档中将对该书的引用添加到此文件中。 After which you can simply create a query to select all books references from this list.之后,您可以简单地创建一个查询 select 此列表中的所有书籍引用。

instead of代替

.where("bookId", isEqualTo: favBooks[0])

I used我用了

.where("bookId", whereIn: favBooks)

and it worked它奏效了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM