假设我有这样一节课
class OrderInfo {
final String orderId;
final String status;
final DateTime orderDateTime;
final DateTime? deliverDateTime;
final String? restaurantTitle;
OrderInfo({
required this.orderId,
required this.status,
required this.orderDateTime,
required this.deliverDateTime,
required this.restaurantTitle,
});
// and a getter
Something get something {
// Very long code that I don't want to recalculate
return something;
};
}
因为所有字段都是final
,所以没有必要重新计算(返回值将是相同的).
我试着像这样创建字段.
late Something something = () {
// Very long code that I don't want to recalculate
return something;
}();
但我不确定这是不是正确的做法. 有其他方法吗?或者这个可以吗?