无法将 json 对象输入 Dart 解析器函数

Can't input json object into Dart parser function

我从 post 请求返回一个 json 对象,并想将其解析为飞镖模型。我已经生成了飞镖模型和 fromJson 函数。

factory Did.fromJson(Map<String, dynamic> json) => Did(
      id: json['id'] as String,
      docHash: json['docHash'] as String,
      pubKey: json['pubKey'] as String,
      privKey: json['privKey'] as String,
      credential:
          Credential.fromJson(json['credential'] as Map<String, dynamic>),
      message: json['message'] as String,
      success: json['success'] as bool);

这只是我的模型的摘录,但我收到错误的部分 The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'。在 bloc 存储库中创建 post 请求后,我试图解析对 dart 对象的响应:

var res = await dio.post("http://did-backend.herokuapp.com/create",
    data: {
      "firstName": firstName.trim(),
      "lastName": lastName.trim(),
      "email": email.trim(),
      "phoneNumber": phoneNumber.trim(),
      "dateOfBirth": dateOfBirth?.toIso8601String(),
      "sex": sex.trim(),
      "address": address.trim(),
      "city": city.trim(),
      "state": state.trim(),
      "postalCode": postalCode.trim(),
      "country": country.trim()
    },
    options: Options(headers: {
      Headers.contentTypeHeader: "application/json",
    }));
if (res.statusCode == 200) {
  final json = jsonDecode(res.data.toString());
  print(Did.fromJson(json));
  return Did.fromJson(json);
}

解码的 json 对象是动态类型,所以我不能将它传递给 Did.fromJson() 函数。
我如何转换 Dio 响应以将其传递给 fromJson 函数?
编辑:
这是我的模型,其中包含 fromJSON 函数 Did.dart
编辑 2:
尚未解析的 Dio post 请求的响应:
print(res)

{
  "id": "GbLnu9eCQ2sVBiKngNxts6NJMprxRczc63CKaZsiJsGT",
  "docHash": "PNSGTXSGODDBHRVWUTFIZUJT9UMPNM9MFBEMCQBYSIRQTDKXPVUCSPNBCXVNGIFEMWSBRUBYARZDA9999",
  "pubKey": "38aqV9FLAn9bXSPm388LcTronqZabEzpWKQpBZRcpPwP",
  "privKey": "A7cq3Z3eCN573wL4QDPR2UjwnDMML6deTf499RnN64zE",
  "credential": {
    "@context": "https://www.w3.org/2018/credentials/v1",
    "id": "http://example.edu/credentials/3732",
    "type": ["VerifiableCredential", "personalInformationCredential"],
    "credentialSubject": {
      "id": "did:iota:GbLnu9eCQ2sVBiKngNxts6NJMprxRczc63CKaZsiJsGT",
      "address": {
        "street": "awdwada",
        "city": "wdwad",
        "state": "awdad",
        "postalCode": "awdwad",
        "country": "wadawd"
      },
      "dateOfBirth": "2021-04-24T00:00:00.000",
      "email": "awdadawd@adwad.co",
      "name": { "first": "wadawd", "last": "awdawd" },
      "phoneNumber": "awdad",
      "sex": "male"
    },
    "issuer": "did:iota:A5STNhet1zgGbbnZCqniokcAdXbZZ2xcE6QWruQmctEs",
    "issuanceDate": "2021-04-24T15:02:41Z",
    "proof": {
      "type": "MerkleKeySignature2021",
      "verificationMethod": "#key-collection",
      "signatureValue": "3RypuceLDTQt1Anb9WdBj7ayPS91EdiYJ6ELPMChgocm.1117tuDcgbUJddXaLoFqvAh8WWeypGnCTuPCDggJ2cMk6AVyJAjHaaCgSmgaKsGa299TxVBqfypgqbjQx1gExf2kkD9XU8ViYhZRVm9dx5qELnVxcM2H5R5YmL6rLn3RR6SbiNSc7XG.22rTApWSHuzuZNFtN75KcsEVqhgzDG3WGoAxs2itVdy99DkKRSVkbCyhJgd1pgAQPPpnt65Sh3m733PsnY6QFojF"
    }
  },
  "message": "You have successfully created your digital identity, wadawd",
  "success": true
}

如果您确定类型,只需通过 as 关键字 (https://dart.dev/guides/language/language-tour#type-test-operators) 进行类型转换使其显式化。

例如:

final json = jsonDecode(res.data.toString()) as Map<String, dynamic>;
print(Did.fromJson(json));

试着把你的fromJson改成这样:

id = json['id'];
docHash = json['docHash'];
pubKey = json['pubKey'];
privKey = json['privKey'];
credential = json['credential'] != null
    ? new Credential.fromJson(json['credential'])
    : null;
message = json['message'];
success = json['success'];

编辑:

我正在尝试向您提出请求 Api 我已经为你生成了模型而不是你的

下载link: Data Model

注意res.data类型-Dio已经解码为json可以通过数据直接到构造函数:

  if (res.statusCode == 200) {
    print(res.data.runtimeType); // _InternalLinkedHashMap<String, dynamic>
    return Did.fromJson(res.data);
  }

仅供参考,您可以使用 responseType 属性 更改响应数据类型,例如:

  var res = await dio.post('http://did-backend.herokuapp.com/create',
      data: {
         // ...
      },
      options: Options(
        responseType: ResponseType.plain,
        headers: {
          // ...
        },
      ));