1

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();
takanopontaro
  • 217
  • 1
  • 2
  • 12
  • Can you give some context of what you are going to do with this? You can ref. to a class in Dart by using the `Type` type like `Type type = Item` but since you cannot do anything with a `Type` instance besides compare two `Type` instances it is not that useful in most situations. Also, you cannot restrict what `Type` you give. – julemand101 May 02 '20 at 07:53
  • @julemand101 thanks for your comment. I added some more context. – takanopontaro May 02 '20 at 09:30

1 Answers1

1

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.

julemand101
  • 28,470
  • 5
  • 52
  • 48
  • Thanks @julemand101! `final RecordBase Function() getRecordClass;` I didn't think of that way. I'm going to try that. – takanopontaro May 02 '20 at 12:51
  • Well, I should properly also add that an alternative is to make `getRecordClass` just a abstract method which must be implemented by all classes extending from `DbBase`. That would work the same. :) – julemand101 May 02 '20 at 15:28