【问题标题】:Flutter Dart: Dynamic Relationship inside Model Class FieldFlutter Dart:模型类字段内的动态关系
【发布时间】:2022-01-23 19:45:09
【问题描述】:

我使用过 Laravel,我们可以在其中定义模型本身内部的关系。那么这在 Dart/Flutter 中是否可行?

我已经构建了这个购物车模型,其中总计应该是动态的。 因此,每当我创建此购物车模型的实例时,一旦我们输入总计和折扣,就会自动计算总计。直到那时默认值 0。

上图代码:

class Cart {
  Cart({
    this.total = 0,
    this.discount = 0,
    this.grandTotal, // I want this field dynamic: (grandtotal = total - discount)
  });

  int total;
  int discount;
  int? grandTotal;
}

【问题讨论】:

    标签: flutter dart model


    【解决方案1】:

    定义一个getter方法来计算grandTotal

    示例代码:

    int? get grandTotal {
        // TODO: check for null cases here
        return total - discount;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 2021-01-29
      • 2020-07-06
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多