'Dart abstract factory method

I'm pulling table data from api in my application that I wrote with Flutter. According to the API used in the table data, different types of data are coming. I created these data types by extending from the abstract class named RecordBase. I am using a factory method as follows to create an object with json data from the class.

factory CaseRecord.fromJson(Map<String, dynamic> json) => CaseRecord(
    field1: json["field1"],
    ...
    fieldx: json["fieldx"],
);

However, where I want to create the object, I need to call this function over the RecordBase class. Sample:

class TableModel <T extends RecordBase> {
    ...
    factory TableModel.fromJson(Map<String, dynamic> json) => TableModel(
        records: List<T>.from(json["records"].map((x) => RecordBase.fromJson(x))),
    );
    ...
}

To be able to call the RecordBase.fromJson(x) constructor from TableModel RecordBase I need to write the following constructor in the class.

factory RecordBase.fromJson(Map<String, dynamic> json);

But the Dart language does not allow this. In short, I want to create an object by typing T.fromJson(x) into TableModel.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source