如何使用键从Firestore加载数据

问题描述 投票:0回答:1

我正在尝试使用Flutter和Firestore构建电子商务应用程序,在构建购物车方面遇到了挑战。使用以下代码,我已经能够使用产品ID将用户希望添加到购物车的产品添加到购物车中。我的挑战是如何使用ID或密钥从Firestore获取产品的详细信息,并将购物车产品存储在Firestore或SQLite中,以便我可以从那里查询并将其显示在购物车页面上。

appstate.dart


class AppStateModel extends Model {

  final Map<int, int> _productsInCart = <int, int>{};

  Map<int, int> get productsInCart => Map<int, int>.from(_productsInCart);



  void addProductToCart(int productId) {
    if (!_productsInCart.containsKey(productId)) {
      _productsInCart[productId] = 1;
    } else {
      _productsInCart[productId]++;
    }

    notifyListeners();
  }


  void removeItemFromCart(int productId) {
    if (_productsInCart.containsKey(productId)) {
      if (_productsInCart[productId] == 1) {
        _productsInCart.remove(productId);
      } else {
        _productsInCart[productId]--;
      }
    }

    notifyListeners();
  }

  void clearCart() {
    _productsInCart.clear();
    notifyListeners();
  }


}

product_display.dart具有onPressed函数的页面以获取单击以添加到购物车的商品的ID

  CupertinoActionSheetAction(
    child: const Text('Add To Cart'),
      onPressed: () {
   model.addProductToCart(products[index].id);
  },
)

product.dart

class Products{
 final  String category;
 final  String description;
 final  int id;
 final int  price;
 final  String title;
 final  String url;


 const Products( {this.category,this.description,this.id, this.price, this.title, this.url,
 });


}

CartProduct.dart

class CartProducts{
 final  String category;
 final  String description;
 final  int id;
 final int  price;
 final  String title;
 final  String url;


 const CartProducts( {this.category,this.description,this.id, this.price, this.title, this.url,
 });


}

现在说我的产品购物车中有ID为1、4、6、9、11的产品,当我在控制台中使用print(model.productsInCart.keys)进行打印时,这就是输出(1、4、6、6, 9、11),现在我面临的挑战是如何使用这些ID从Firestore集合产品中查询ID为1、4、6、9、11的产品,并将其存储在Firebase或SQLite中,以便可以在购物车中显示它们用户查看购物车中的物品的页面。

firestore database

flutter google-cloud-firestore shopping-cart
1个回答
0
投票

我认为这是您想要做的

© www.soinside.com 2019 - 2024. All rights reserved.