0

Here is my object

class CustomerDomain {
  String customerID;
  String citizenID;
  String status;
  String title;
  String name;
  String email;
  String phoneNumber;
  Map<String, ServiceDetail> serviceDetails;
  int remainingMinute;
  Map<String, ReferenceChannel> referenceChannels;
  String omiseCustomerID;

  CustomerDomain({
    this.customerID,
    this.citizenID,
    this.status,
    this.title,
    this.name,
    this.email,
    this.phoneNumber,
    this.serviceDetails,
    this.remainingMinute,
    this.referenceChannels,
    this.omiseCustomerID,
  });

  factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
    return CustomerDomain(
      customerID: parsedJson['customerID'],
      citizenID: parsedJson['citizenID'],
      status: parsedJson['status'],
      title: parsedJson['title'],
      name: parsedJson['name'],
      email: parsedJson['email'],
      phoneNumber: parsedJson['phoneNumber'],
      serviceDetails: parsedJson['serviceDetailsails'],
      remainingMinute: parsedJson['remainingMinute'],
      referenceChannels: parsedJson['referenceChannels'],
      omiseCustomerID: parsedJson['omiseCustomerID'],
    );
  }
}

After calling a service, I return my response like this

if (response.statusCode == 200) {
      print('entranceService3');
      return CustomerDomain.fromJson(json.decode(response.body));
    }

I printing the value like customerID and it works. But I cannot use print the value in the ReferenceChannel from referenceChannels. When I convert referenceChannels into list and then string, and print it. I got something like this

[{channel:channel1,code:code1,secondCode:code2}]

So, I think that I didn't map the json correctly because the value that has type Map<String, Object> didn't work properly when I try to the value of the object.

1
  • You can manually handle collections inside JSON and format it to a object that you need. Commented Jan 22, 2021 at 14:40

3 Answers 3

1

make sure, that your JSON string is a proper JSON string, in your case, the string should be as following:

'[{"channel":"channel1","code":"code1","secondCode":"code2"}]'

check out the snippet, I used the JSON.parse function and it works.

let jsonString = '[{"channel":"channel1","code":"code1","secondCode":"code2"}]';
let jsonArray = JSON.parse(jsonString);

console.log('channel name: 'jsonArray[0].channel);
console.log(jsonArray);

Sign up to request clarification or add additional context in comments.

Comments

0

As you have already known that you cannot do something like this:

customDomain: parsedJson

but instead you need a CustomDomain.fromJson to map the value and ensure the type-safety.

So the same concept applies to ServiceDetailsails and ReferenceChannels as well. You will need to parse the whole object like:

factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
return CustomerDomain(
  customerID: parsedJson['customerID'],
  citizenID: parsedJson['citizenID'],
  status: parsedJson['status'],
  title: parsedJson['title'],
  name: parsedJson['name'],
  email: parsedJson['email'],
  phoneNumber: parsedJson['phoneNumber'],
  serviceDetails: ServiceDetailsails.fromJson(parsedJson['serviceDetailsails']),
  remainingMinute: parsedJson['remainingMinute'],
  referenceChannels: ReferenceChannels.fromJson(parsedJson['referenceChannels']),
  omiseCustomerID: parsedJson['omiseCustomerID'],
);}

If you are using dart, you can also check this doc for more details: https://flutter.dev/docs/development/data-and-backend/json

Comments

0

Map is also a Key Value like JSON so it would look something

{
"referenceChannels": {
"key":"value"
}

} if your value is another object then

"key":{"key1":"value","key1":"value"  }

Overall something like this:

{
"referenceChannels": {
"key":{"key1":"value","key2":"value"  }
}

You will have to iterate through the Reference Channel Object JSON like you did for CustomerDomain. You can also create a FromJson() method in Reference Channel class and use that in the fromJson of Customer Domain like this:

 factory CustomerDomain.fromJson(Map<String, dynamic> parsedJson) {
    return CustomerDomain(
// Add other fields here 
      referenceChannels: parsedJson['referenceChannels'].fromJson,
  
    );
  }

Also try creating the class here https://app.quicktype.io/. This website will create your class with toJson() and FromJson() methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.