Flutter Dart:Model Class Field 内部的动态关系
Flutter Dart: Dynamic Relationship inside Model Class Field
我使用了 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;
}
定义一个getter方法来计算grandTotal
.
示例代码:
int? get grandTotal {
// TODO: check for null cases here
return total - discount;
}
我使用了 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;
}
定义一个getter方法来计算grandTotal
.
示例代码:
int? get grandTotal {
// TODO: check for null cases here
return total - discount;
}