如何将 Firebase 中的 collection 转换为字符串列表并使用它设置一些条件

How to turn a collection in Firebase into a List of Strings and make some conditions with it

我正在尝试在 Firebase 中获取 collection 并将其转换为字符串列表并为其设置一些条件。我正在为一家商店创建一个应用程序,我的目的是在我按下产品中的按钮时,应用一种方法为每个用户创建一个收藏夹列表。要在 firebase 中添加最喜欢的产品,我正在使用它,它正在运行:

_saveFavorite(Product product) async {

    await _loadCurrentUser(); //So I get the user ID

    Firestore db = Firestore.instance;
    db.collection("my_favorites")
        .document(_userID)
        .collection("products")
        .document(product.id)
        .setData(product.toMap());
  }

要删除最喜欢的产品,我使用的是:

_removeFavorite(String productID) async {
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    db.collection("my_favorites")
        .document(_userID)
        .collection("products")
        .document(product.id)
        .delete();
  }

所以,这是这样的:Collection("my_favorites") > Document (userID) > Collection ("products") > Document (productID) > products save作为收藏。

我正在尝试获取 Collection("products") 中保存的所有 productsID 来为凸起按钮设置条件,但我不知道该怎么做。我想按下此按钮并设置如下条件: ListOfIDProducts.contains(product.id) ? _removeFavorite : _saveFavorite;

感谢您的关注,如果您能帮助我,我将不胜感激!

您可以将这样的列表存储在 /my_favorites/USER_ID 文档中,作为当前最喜欢的产品 ID 的数组。您可以在 /my_favorites/USER_ID/products collection 中添加和删除每个产品时使用 Cloud Function 维护此列表,但可以说仅使用 batched write along with the array field transforms, arrayUnion() and arrayRemove().

更简单
_saveFavorite(Product product) async {
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    WriteBatch batch = db.batch();
    // queue adding the product's ID to the products array
    batch.update(
        db.collection("my_favorites")
            .document(_userID),
        {
            products: FieldValue.arrayUnion([product.id])
        }
    );
    // queue uploading a copy of the product's data to this user's favorites
    batch.set(
        db.collection("my_favorites")
            .document(_userID)
            .collection("products")
            .document(product.id),
        product.toMap()
    );
    return batch.commit();
}

_removeFavorite(String productID) async {
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    WriteBatch batch = db.batch();
    // queue removing product.id from the products array
    batch.update(
        db.collection("my_favorites")
            .document(_userID),
        {
            products: FieldValue.arrayRemove([product.id])
        }
    );
    // queue deleting the copy of /products/PRODUCT_ID in this user's favorites
    batch.delete(
        db.collection("my_favorites")
            .document(_userID),
            .collection("products")
            .document(product.id)
    );
    return batch.commit();
}

要获取产品 ID 列表,您可以使用类似于以下内容的内容:

_getFavoriteProductIDs() async {
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    return db.collection("my_favorites")
        .document(_userID)
        .get()
        .then((querySnapshot) {
            return querySnapshot.exists ? querySnapshot.get("products") : []
        });
}

您甚至可以将其转换为使用列表来代替:

_saveFavorite(List<Product> products) async {
    if (products.length == 0) {
      return; // no action needed
    }
    
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    WriteBatch batch = db.batch();
    // queue adding each product's ID to the products array
    batch.update(
        db.collection("my_favorites")
            .document(_userID),
        {
            products: FieldValue.arrayUnion(
                products.map((product) => product.id).toList()
            )
        }
    );
    // queue uploading a copy of each product to this user's favorites
    for (var product in products) {
        batch.set(
            db.collection("my_favorites")
                .document(_userID)
                .collection("products")
                .document(product.id),
            product.toMap()
        );
    }
    return batch.commit();
}

_removeFavorite(List<String> productIDs) async {
    if (productIDs.length == 0) {
      return; // no action needed
    }
    
    await _loadCurrentUser();

    Firestore db = Firestore.instance;
    WriteBatch batch = db.batch();
    // queue removing each product ID from the products array
    batch.update(
        db.collection("my_favorites")
            .document(_userID),
        {
            products: FieldValue.arrayRemove(productIDs)
        }
    );
    // queue deleting the copy of each product in this user's favorites
    for (var product in products) {
        batch.delete(
            db.collection("my_favorites")
                .document(_userID)
                .collection("products")
                .document(product.id)
        );
    }
    return batch.commit();
}

补充说明:在您当前的实施中,收藏的产品从 /products/PRODUCT_ID 复制到 /my_favorites/USER_ID/products/PRODUCT_ID。请记住,使用此结构,如果更新 /products/PRODUCT_ID,您将必须更新该产品的每个副本。我建议将 products 重命名为 favorited-products,这样您就可以使用 Cloud Function and a Collection Group Query (see this answer 来获得更多信息)。