调用私有构造函数-构造函数不存在

时间:2020-06-10 22:06:44

标签: flutter dart

我看到了这个answer,并且正在尝试将其复制到我的代码中。

下面是基类:

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();
  }
}

这是派生类:

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;
  }
}

在派生类内部,super._fromJson(json)引发以下错误:

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.

谢谢。

0 个答案:

没有答案