The sort answer if you want to upload your data object is to convert it to a string like so:
var jsonString = json.encode(data);
// upload jsonString as body to your server.
print("Json String is " + jsonString);
Map headers = {
'Content-type' : 'application/json',
};
http.post("www.server.com/myPostEndpoint", body: jsonString, headers: headers);
Like discussed in the comments you may need to wrap the entire content in a data property. You may try this:
var payload = {
"data": {
"email": "[email protected]",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "[email protected]",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
}
};
var jsonString = json.encode(payload); // upload jsonString as body to your server. print("Json String is " + jsonString);
Map headers = { 'Content-type': 'application/json', };
http.post("www.server.com/myPostEndpoint", body: jsonString, headers: headers);
tl;dr
Its always a good idea to create a Model.
So this could be your User model:
class User {
final String email;
final String password;
final String firstName;
final Map<String, String> billing;
final Map<String, String> shipping;
User({this.email, this.password, this.firstName, this.billing, this.shipping});
static User fromJson(dynamic json) {
return User(
email: json["email"],
password: json["password"],
firstName: json["first_name"],
billing: json["billing"],
shipping: json["shipping"],
);
}
}
It has a static constructor which is handy for creating an instance of this User from a piece of decoded json like you have.
Now you can go ahead and use this model in your code like so:
var data = {
"email": "[email protected]",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "[email protected]",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
};
var user = User.fromJson(data);
print("Users Email is ${user.billing["email"]}");
It would be even cleaner to also create models for both Billing and maybe also Shipping.
Here is a complete example which also includes the toJson() methods to generate Json from a parsed model:
class User {
final String email;
final String password;
final String firstName;
final Billing billing;
final Shipping shipping;
User({this.email, this.password, this.firstName, this.billing, this.shipping});
static User fromJson(dynamic json) {
return User(
email: json["email"],
password: json["password"],
firstName: json["first_name"],
billing: Billing.fromJson(json["billing"]),
shipping: Shipping.fromJson(json["shipping"]),
);
}
Map<String, dynamic> toJson() => {'email': email, 'password': password, 'first_name': firstName, 'billing': billing.toJson(), 'shipping': shipping.toJson()};
}
class Billing {
final String email;
final String phone;
Billing({this.email, this.phone});
static Billing fromJson(dynamic json) {
return Billing(email: json["email"], phone: json["phone"]);
}
Map<String, dynamic> toJson() => {'email': email, 'phone': phone};
}
class Shipping {
final String name;
Shipping({
this.name,
});
static Shipping fromJson(dynamic json) {
return Shipping(
name: json["first_name"],
);
}
Map<String, dynamic> toJson() => {'name': name};
}
And this is how you could use it:
var data = {
"email": "[email protected]",
"password": "123",
"first_name": "qweqwe",
"billing": {
"email": "[email protected]",
"phone": "321654978",
},
"shipping": {
"first_name": "qweqwe",
}
};
var user = User.fromJson(data);
print("Users Email is ${user.billing.email}");
print("Shipping name is ${user.shipping.name}");
var jsonString = json.encode(user.toJson()); // upload this as body to your server.
print("Json String is " + jsonString);