【问题标题】:Append element to a list flutter firebase将元素附加到列表颤振火力基地
【发布时间】:2021-11-26 00:27:14
【问题描述】:

我目前正在使用 FLutter 和 firebase。我正在尝试将一些数据上传到云 Firestore。到现在为止还挺好。当我想将列表附加到字段时,我遇到了问题。 如前所述,此字段包含定义如下的值列表:

Map<String, Object> toDocument() {
  Map customerAddress = Map();
  Map orderCheckoutDetails = Map();

  final DateTime now = DateTime.now();
  final DateFormat formatter = DateFormat('dd-MM-yyyy');
  final String formatted = formatter.format(now);

  customerAddress['address'] = this.address;
  customerAddress['city'] = this.city;
  customerAddress['state'] = this.state;
  customerAddress['zipCode'] = this.zipCode;

  orderCheckoutDetails['checkoutOrderDate'] = formatted;
  orderCheckoutDetails['customerAddress'] = customerAddress;
  orderCheckoutDetails['customerName'] = '${this.nome!} ${this.cognome!}';
  orderCheckoutDetails['customerPhone'] = this.numeroTelefono!;
  orderCheckoutDetails['products'] = this.products!.map((product) => product.name).toList();
  orderCheckoutDetails['subtotal'] = this.subtotal!;
  orderCheckoutDetails['deliveryFee'] = this.deliveryFee!;
  orderCheckoutDetails['total'] = this.total!;
  List<Map<dynamic, dynamic>> orderList = [orderCheckoutDetails];

  return {
    'orderCheckoutDetails': orderList,
  };
}

这就是它在 Firebase 上向我显示该项目的方式(这是正确的)。 enter image description here

这就是我将文档上传到 Firebase 的方式。

 @override
 Future<void> addCheckout(Checkout checkout) async {
  print(checkout.email!);

  final docExists = await _checkIfUserCheckoutExists(checkout.email!);
  print(docExists);

  if (!docExists) {
  return _checkoutCollection
      .doc(checkout.email!)
      .set(checkout.toDocument());
  } else {
    await _checkoutCollection.doc(checkout.email!).update({
    "orderCheckoutDetails":
        FieldValue.arrayUnion(checkout.toDocument() as List)
  });
}

}

我想做的是在文档末尾附加另一个元素(通过参数传递的结帐元素)。我该怎么做?

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您可以在两种情况下(无论文档是否存在)使用带有合并选项的set,它会根据需要创建或更新文档。另外,您的 toDocument 方法应该返回 orderList 本身,而不是您当前返回的地图。

    试试这个:

    Map<dynamic, dynamic> toDocument() {
      Map customerAddress = Map();
      Map<dynamic, dynamic> orderCheckoutDetails = Map();
    
      final DateTime now = DateTime.now();
      final DateFormat formatter = DateFormat('dd-MM-yyyy');
      final String formatted = formatter.format(now);
    
      customerAddress['address'] = this.address;
      customerAddress['city'] = this.city;
      customerAddress['state'] = this.state;
      customerAddress['zipCode'] = this.zipCode;
    
      orderCheckoutDetails['checkoutOrderDate'] = formatted;
      orderCheckoutDetails['customerAddress'] = customerAddress;
      orderCheckoutDetails['customerName'] = '${this.nome!} ${this.cognome!}';
      orderCheckoutDetails['customerPhone'] = this.numeroTelefono!;
      orderCheckoutDetails['products'] = this.products!.map((product) => product.name).toList();
      orderCheckoutDetails['subtotal'] = this.subtotal!;
      orderCheckoutDetails['deliveryFee'] = this.deliveryFee!;
      orderCheckoutDetails['total'] = this.total!;
    
      return orderCheckoutDetails;
    }
    

    然后创建/插入您的文档,例如:

    return _checkoutCollection
      .doc(checkout.email!)
      .set({
        'orderCheckoutDetails' : FieldValue.arrayUnion([checkout.toDocument])
       }, SetOptions(merge: true));
    

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2021-03-10
      • 2021-07-03
      • 2021-03-19
      • 2021-02-03
      • 1970-01-01
      • 2020-06-26
      • 2020-07-28
      • 2022-01-14
      相关资源
      最近更新 更多