0

我有两个类:BaseClass (BaseResponse) 和 DerivedClass (LoginResponse)。我正在使用 fromJson 方法对它们进行反序列化。

我想了解如何为基类调用 fromJson 方法。

class BaseResponse { 
  int responseCode;
  String responseMessage;

  BaseResponse.fromJson(Map<String, dynamic> input)
      : responseCode = input["ResponseCode"],        
        responseMessage = input["ResponseMessage"];
}
class LoginResponse extends BaseResponse {
    String authenticationToken;
    
    LoginResponse.fromJson(Map<String, dynamic> input) 
      : authenticationToken = input["AuthenticationToken"];

}

LoginResponse.fromJson(Map<String, dynamic> input) 给我一个错误说:类'BaseResponse'没有未命名的构造函数。

任何帮助表示赞赏。

谢谢。

4

3 回答 3

1

在基类中定义构造函数。

BaseResponse({required this.responseCode, required this.responseMessage});

派生类中

LoginResponse(
      {required this.authenticationToken,
      required int responseCode,
      required String responseMessage})
      : super(responseCode: responseCode, responseMessage: responseMessage);

添加LoginResponse.fromJsonasfactory并使用LoginResponse.

factory LoginResponse.fromJson(Map<String, dynamic> input) => LoginResponse(
          authenticationToken: input['AuthenticationToken'],
          responseCode: input['ResponseCode'],
          responseMessage: input['ResponseMessage']);

类应该是这样的:

class BaseResponse {
  int responseCode;
  String responseMessage;

  BaseResponse({required this.responseCode, required this.responseMessage});

  BaseResponse.fromJson(Map<String, dynamic> input)
      : responseCode = input["ResponseCode"],
        responseMessage = input["ResponseMessage"];
}

class LoginResponse extends BaseResponse {
  String authenticationToken;

  LoginResponse(
      {required this.authenticationToken,
      required int responseCode,
      required String responseMessage})
      : super(responseCode: responseCode, responseMessage: responseMessage);

  factory LoginResponse.fromJson(Map<String, dynamic> input) => LoginResponse(
      authenticationToken: input['AuthenticationToken'],
      responseCode: input['ResponseCode'],
      responseMessage: input['ResponseMessage']);
}
于 2021-08-19T11:48:49.757 回答
0

通过 BaseResponse 发布数据使用 url,参数 json 正文:

Future<dynamic> postData(String _url, dynamic _body, {@required latlng = null}) async {
    BaseResponse responseJson;
    try {
      Uri url = Uri.parse(_url);
      latlng = latlng ?? '93.2548,23.5987';
      var headers = {"latLng": "$latlng", "Content-Type": "application/json", 'Authorization': 'Bearer ${CtsSharedPreferences.getString('token')}'};
      final response = await post(url, headers: headers, body: _body);
      responseJson = _returnResponse(response);
    } on SocketException {
      throw FetchDataException('No Internet connection');
    }
    return responseJson;
  }
于 2021-12-21T08:48:14.790 回答
0

除非您出于某种原因绝对需要它,否则我不会为 BaseResponse 制作 .fromJson 。

我认为更好的方法是在基类中声明最终变量并在那里断言表达式,并且只在派生类中实现模型或任何类型:

class BaseResponse { 
  final int responseCode;
  final String responseMessage;

  BaseResponse({@required this.responseCode, @required this.responseMessage)
  : assert(<expression>);
}

class LoginResponse extends BaseResponse {
  final String authenticationToken;
  final int code;
  final String message;
   
  LoginResponse({
    this.authenticationToken,
    this.code,
    this.message,
  }) : super(code: code, message: message)

  LoginResponse.fromJson(Map<String, dynamic> input) {
    return LoginResponse(
    authenticationToken: input['token'],
    code: input['code'],
    message: input['message'],
    );
  }

}
于 2020-10-14T11:23:27.043 回答