我想从我的购物车中将数据添加到Firestore中,在其中通过StreamBuilder添加数据

时间:2019-09-01 20:45:05

标签: flutter google-cloud-firestore

我正在为我的大学项目构建一个餐厅应用程序,其中我使用了来自thecsguy的youtube视频中的概念,该应用程序可在blocpattern和streambuilder上使用,因此主页上有fooditem,当我点击它们时,它们就会添加到购物车中,因此,当我转到购物车并单击“下订单”时,应将购物车中的商品添加到firestore中,但我不知道该如何使用我的代码。

This is where the items are added to cart

Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        addToCart(foodItem); //the functions which adds the fooditem 
                               to cart where foodItem is the object 
                               of class FoodItem which is the model 
                               of fooditems(the fooditems which will 
                               be added to cart). 
....rest of UI code....
        }
    }
}

addToCart(foodItem)函数为:

  addToCart(FoodItem foodItem) {
    bloc.addToList(foodItem);
  }

这里的“ bloc”是:

final CartListBLoc bloc = BlocProvider.getBloc<CartListBLoc>();

其中,CartListBloc类为:

class CartListBLoc extends BlocBase {
CartListBLoc();
var _listController = BehaviorSubject<List<FoodItem>>.seeded([]);
CartProvider provider = CartProvider();
//output-
Stream<List<FoodItem>> get listStream => _listController.stream;
//input
Sink<List<FoodItem>> get listSink => _listController.sink;
//logic
addToList(FoodItem foodItem){
  listSink.add(provider.addToList(foodItem));
}
removeFromList(FoodItem foodItem){
  listSink.add(provider.removeFromList(foodItem));
}
//dispose will be called automatically by closing its streams
@override
void dispose() {
  _listController.close();
  super.dispose();
}
}

购物车的主体包含一个列表视图:

ListView foodItemList() {
    return ListView.builder(
      itemCount: foodItems.length,
      itemBuilder: (builder, index) {
        return CartListItem(foodItem: foodItems[index]); //this class
                                                         is given below
      },
    );
  }
class CartListItem extends StatelessWidget {
  final FoodItem foodItem;
  CartListItem({@required this.foodItem});
  @override
  Widget build(BuildContext context) {
    return Draggable(
      data: foodItem,
      maxSimultaneousDrags: 1,
      child: new DraggableChild(foodItem: foodItem),
      feedback: DraggableChildFeedBack(foodItem: foodItem),
      childWhenDragging: foodItem.quantity > 1
          ? DraggableChild(
              foodItem: foodItem,
            )
          : Container(),
    );
  }
}

忽略可拖动的孩子和可拖动的孩子,但请任何人快速帮助我

0 个答案:

没有答案