【问题标题】:How to put duration in firestore flutter?如何将持续时间放在 Firestore Flutter 中?
【发布时间】:2021-10-08 12:16:33
【问题描述】:

我正在为 Flutter 中的电子商务应用开发一个学校项目。

我需要在我的应用程序中保存准备食物的“持续时间”并从 Firestore 中检索它。我需要将其保存为时间戳吗?如果是这样,我将如何检索它并将其再次转换为持续时间,以便显示食物准备的分钟或小时?

这是我的代码示例:

class ProductModel { 
  String? productName; 
  String? prdouctImgUrl; 
  double? productPrice; 
  // Duration? preparationTime; ===> the preparation time of the food
  
  ProductModel();
  ProductModel.fromSnapshot(DocumentSnapshot snap){
  productName = snap["productName"];
  prdouctImgUrl= snap["prdouctImgUrl"];
  productPrice= snap["productPrice"];
  //preparationTime= snap["preparationTime"]; 
  }

  }

  //showing of products
  .....
  _product = ProductModel.fromSnapshot(snapshot.data!.docs[i]);
  return ListTile(
  leading: CircleAvatar(
  backgroundImage: NetworkImage(_product.prdouctImgUrl),
  child: _product.prdouctImgUrl.isEmpty
  ? Text("${_product.productName[0].toUpperCase()}") : SizedBox(),
  radius: 15,),
  title: Text(_product.productName),
  //subtitle: Text("Preparation time ${_product.preparationTime]} \n Expected delivery date: ${DateTime.now().add(_product.preparationTime])}"), 
  // ???? I want to show how many minutes the preparation time is and when can the customer receive the items

)

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    您可以使用intl package 随意格式化日期时间。

    例如:

    var timeHourMinute = DateFormat('H:m').format(DateTime.now()); // 12:30
    var timeMinute = DateFormat('m').format(DateTime.now()); // 30
    

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 2021-05-16
      • 1970-01-01
      • 2023-02-14
      • 2019-09-22
      • 2019-10-30
      • 1970-01-01
      • 2021-04-17
      • 2016-07-17
      相关资源
      最近更新 更多