Can't input json object into Dart parser function

Issue

I’m returning a json object from a post request and want to parse it to a dart model. I have already generated the dart model and fromJson function.

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

This is only an extract from my model but the part were I get the error The argument type 'dynamic' can't be assigned to the parameter type 'Map<String, dynamic>'. I’m tryingto parse the reponse to the dart object after creating the post request in the bloc repository:

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

The decoded json object is of type dynamic so I can’t pass it into the Did.fromJson() function.
How do I have to transform the Dio response to pass it into the fromJson function?
Edit:
This is my model which includes the fromJSON function
Did.dart
Edit 2:
The response of the Dio post request which is not parsed yet:
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
}

Solution

If you’re sure of the type, you just need to make it explicit by typecasting via the as keyword (https://dart.dev/guides/language/language-tour#type-test-operators).

For example:

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

Answered By – Giovanni Londero

Answer Checked By – Candace Johnson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *