dartdart-2

What is an equivalent for Dart 2 to `typeof` of TypeScript?


I'm new to Dart 2. I want a class to have a property. It's a reference of other class. it's not an instance but class itself. In TypeScript, it's possible to write as below. Is there a same way in Dart 2?

class Item { }

class ItemList {
    itemClass: typeof Item;
}

const itemList = new ItemList();
itemList.itemClass = Item;

UPDATED:
I added some more context. The following is minimal sample code. I want to delegate a role of instantiation to super class.

class RecordBase {
    id = Math.random();
    toJson() {
        return { "id": this.id };
    };
}

class DbBase {
    recordClass: typeof RecordBase;
    create() {
        const record = new this.recordClass();
        const json = record.toJson();
        console.log(json);
    }
}

class CategoryRecord extends RecordBase {
    toJson() {
        return { "category": "xxxx", ...super.toJson() };
    };
}

class TagRecord extends RecordBase {
    toJson() {
        return { "tag": "yyyy", ...super.toJson() };
    };
}

class CategoryDb extends DbBase {
    recordClass = CategoryRecord;
}

class TagDb extends DbBase {
    recordClass = TagRecord;
}

const categoryDb = new CategoryDb();
categoryDb.create();

const tagDb = new TagDb();
tagDb.create();

Solution

  • I have tried to make you sample code into Dart. As I told before, you cannot get a reference to a class and call the constructor on runtime based on this reference.

    But you can make a reference to a method which constructs the object of you class.

    import 'dart:math';
    
    class RecordBase {
      static final Random _rnd = Random();
    
      final int id = _rnd.nextInt(100000);
    
      Map<String, dynamic> toJson() => <String, dynamic>{'id': id};
    }
    
    abstract class DbBase {
      final RecordBase Function() getRecordClass;
    
      RecordBase record;
      Map<String, dynamic> json;
    
      DbBase(this.getRecordClass);
    
      void create() {
        record = getRecordClass();
        json = record.toJson();
        print(json);
      }
    }
    
    class CategoryRecord extends RecordBase {
      @override
      Map<String, dynamic> toJson() {
        return <String, dynamic>{'category': 'xxxx', ...super.toJson()};
      }
    }
    
    class TagRecord extends RecordBase {
      @override
      Map<String, dynamic> toJson() {
        return <String, dynamic>{'tag': 'yyyy', ...super.toJson()};
      }
    }
    
    class CategoryDb extends DbBase {
      CategoryDb() : super(() => CategoryRecord());
    }
    
    class TagDb extends DbBase {
      TagDb() : super(() => TagRecord());
    }
    
    void main() {
      final categoryDb = CategoryDb();
      categoryDb.create(); // {category: xxxx, id: 42369}
    
      final tagDb = TagDb();
      tagDb.create(); // {tag: yyyy, id: 97809}
    }
    

    I am not really sure if the create() method should be seen as a method or a constructor. So I choose to make it a method to be closer to your code.