1

I saw this answer and I am trying to replicate it in my code.

Below is the base class:

class ResponseObjectBase<T> {
  bool success;
  T responseObject;

  ResponseObjectBase._fromJson(Map<String, dynamic> json){
    success = true;
  }

  factory ResponseObjectBase.fromJson(Map<String, dynamic> json) {
    if (T == OsGridRefModel) {
      return OsGridRefModel.fromJson(json) as ResponseObjectBase<T>;
    }
    throw UnimplementedError();
  }
}

And here is the derived class:

class OsGridRefModel extends ResponseObjectBase<OsGridRefModel>{
  String descriptor;
  double easting;
  double northing;

  OsGridRefModel.fromJson(Map<String, dynamic> json) : super._fromJson(json) { 
        this.descriptor = json['descriptor'] as String;
        this.northing = json["northing"] as double;
        this.easting = json["easting"] as double;
        this.responseObject = this;
  }
}

Inside of the derived class the super._fromJson(json) is throwing the following error:

The class 'ResponseObjectBase<OsGridRefModel>' doesn't have a constructor named '_fromJson'.
Try defining a constructor named '_fromJson' in 'ResponseObjectBase<OsGridRefModel>', or invoking a different constructor.

Thanks.

John
  • 126
  • 6
  • I don't see any issues with your code. How are you using this? – Sami Haddad Jun 10 '20 at 22:23
  • Your use of "parent class" is confusing. You mean "derived class". If the code for your derived class lives in a separately Dart library (usually this means in a separate `.dart` file) from the base class, then it will not be able to use the private base class constructor. A private constructor is usable only within that library. – jamesdlin Jun 10 '20 at 23:10

0 Answers0