Dart: Inheritance, Factory, Enums & Equality

How I Use the Above using a Shopping App Example

Abstract Class & Inheritance

abstract class Product {
final String productUid;
final String productName;
final Gender gender;
final ProductType type;
final bool isOnSale;
final double originalPrice;
final double salePercentage;

const Product({
required this.productUid,
required this.productName,
required this.gender,
required this.type,
required this.isOnSale,
required this.originalPrice,
required this.salePercentage
class Order {
final List<Product> products;
/// and other fields

const Order({required this.products});


class Shoes extends Product {
final ShoeType shoeType;
final int size;

const Shoes({
required super.productUid,
required super.productName,
required super.gender,
required super.type,
required super.originalPrice,
required super.salePercentage,
required this.size,
required this.shoeType,
}) : super (isOnSale: salePercentage > 0.0 ? true : false); // super constructor

class Pants extends Product {
final PantsType pantsType;
final ApparelSize size;

const Pants({required super.productUid,// ...etc});



import 'dart:typed_data';

class ImageService {
Future<Uint8List?> pickPhoto() async {
// some code that returns user's photo from gallery

Future<Uint8List?> takePicture() async {
// some code that opens user camera and returns the photo taken

class ReviewProvider with ImageService {
List<Uint8List> photos = [];

Future<void> pickPhotoFromGallery() async {
final Uint8List? _photo = await super.pickPhoto();
if (_photo == null) return;

Future<void> openCamera() async {
final Uint8List? _photo = await super.pickPhoto();
if (_photo == null) return;

class UserProvider with ImageService {

Future<void> pickPhotoFromGallery() async {
final Uint8List? _photo = await super.pickPhoto();
if (_photo == null) return;
/// some code that changes user's photo


typedef Json = Map<String, dynamic>;

class Shoes extends Product {
// same code as above...
factory Shoes.fromJson(Json json) => Shoes(
productName: json["productName"],
productUid: json["productUid"],
gender: Gender.values.singleWhere((Gender gender) => gender.name == json["gender"]),
size: int.parse(json["size"]),
type: ProductType.values.singleWhere((ProductType type) => type.name == json["productType"]),
shoeType: ShoeType.values.singleWhere((ShoeType type) => type.name == json["shoeType"]),
originalPrice: double.parse(json["price"]),
salePercentage: double.parse(json["salePercentage"]),
class User {
final String userUid;
final String userName;
final String email;

const User({required this.userUid, required this.userName, required this.email});

factory User.changeUserName({required User user, required String newName}) => User(
userUid: user.userUid, email: user.email, userName: newName
class JsonMethod {
static List<T> convertJsonList<T>({required List<dynamic> jsonList, required T Function(Json) factory}) {
final List<Json> _jsonList = List<Json>.from(jsonList);
List<T> _list = [];
_jsonList.forEach((Json json) => _list.add(factory(json)));
return _list;


enum ProductType {
shoes, pants, shorts, shirt, hoodie

extension ProductTypeExt on ProductType {
String get koreanName {
switch (this) {
case ProductType.hoodie: return "후디";
case ProductType.pants: return "바지";
case ProductType.shoes: return "신발";
case ProductType.shirt: return "티셔츠";
case ProductType.shorts: return "반바지";

class Shoes extends Product {
final ProductType type; ///....other code

final Shoes _nikeShoes = Shoes(ProductType.shoes);
final String _typeInKorean = _nikeShoes.type.koreanName;


class Shoes extends Product {
//...same code as above...

bool operator == (other) {
if (other is! Shoes) return false;
if (this.productName != other.productName || this.type != other.type) return false; /// and so on...
return this.productUid == other.productUid && this.size == other.size;

int get hashCode => this.productUid.hashCode + this.size;



Flutter & Node.js Full-Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store