← → or space · progress saves for Continue on the roadmap
Goal
Define three value-style models with final fields and no mutating setters.
Step 1 - User
class User {
final String id;
final String name;
final String email;
const User({
required this.id,
required this.name,
required this.email,
});
}
void main() {
const User u = User(id: '1', name: 'Asha', email: 'a@example.com');
print(u.name);
}Step 2 - Product
class Product {
final String sku;
final String name;
final double price;
const Product({
required this.sku,
required this.name,
required this.price,
});
}Step 3 - OrderItem
class OrderItem {
final String productSku;
final int quantity;
final double unitPrice;
const OrderItem({
required this.productSku,
required this.quantity,
required this.unitPrice,
});
double get lineTotal => unitPrice * quantity;
}Step 4 - Wire in main
void main() {
const User u = User(id: '1', name: 'Asha', email: 'a@example.com');
const Product p = Product(sku: 'SKU-1', name: 'Mug', price: 9.99);
const OrderItem line = OrderItem(
productSku: p.sku,
quantity: 2,
unitPrice: p.price,
);
print('${u.name} buys ${line.quantity} for ${line.lineTotal}');
}Practice tasks
- Add lightweight validation in factories or constructors (reject empty
sku, non-positivequantity) usingArgumentErroror early returns, still without mutable fields. - Add
String describe()on each type returning one readable line. - Keep
OrderItemindependent ofProducttype (sku + prices only) vs hold aProductreference; try both and note tradeoffs.