Hi I have a class that have other classes nested in it. I want to covert an object of this into a JSON string and send it to the server.
I have tried many answers both from stack overflow and google searches. Non sufficiently answer my question.
Any help is appreciated guys.
here are my models
class Place {
String name;
String description;
List<PhoneNumber> phoneNumbers;
List<String> tags;
GPSCoordinante gpsCoordinates;
List<Service> services;
List<Album> albums;
SocialMedia socialMedia;
List<Comment> comments;
List<String> imageURLArray;
int rating;
int shares;
int favorites;
int views;
String category;
String subcategory;
WorkingHour workingHours;
bool deleted;
double distanceToUser;
bool isApproved;
String address;
List<String> coverImages;
Place({
this.name,
this.description,
this.phoneNumbers,
this.tags,
this.gpsCoordinates,
this.services,
this.albums,
this.socialMedia,
this.comments,
this.imageURLArray,
this.rating,
this.shares,
this.favorites,
this.views,
this.category,
this.subcategory,
this.workingHours,
this.deleted,
this.distanceToUser,
this.isApproved ,
this.address,
this.coverImages,
});
factory Place.fromJson(Map<String, dynamic> json) {
List phoneNumbersJsonList = json['phoneNumbers'] as List;
List<PhoneNumber> parsedPhoneNumbers = phoneNumbersJsonList.map((value) => PhoneNumber.fromJson(value)).toList();
List servicesJsonList = json['services'] as List;
List<Service> parsedServices = servicesJsonList.map((value) => Service.fromJson(value)).toList();
List walbumsJsonList = json['albums'] as List;
List<Album> parsedAlbums = walbumsJsonList.map((value) => Album.fromJson(value)).toList();
List commentsJsonList = json['comments'] as List;
List<Comment> parsedComments = commentsJsonList.map((value) => Comment.fromJson(value)).toList();
return Place(
name: json['name'],
description: json['description'],
phoneNumbers: parsedPhoneNumbers,
gpsCoordinates: json['gpsCoordinates'],
services: parsedServices,
albums: parsedAlbums,
socialMedia: json['socialMedia'],
comments: parsedComments,
imageURLArray: json['imageURLArray'],
rating: json['rating'],
shares : json['shares'],
favorites: json['favorites'],
category: json['category'],
subcategory: json['subcategory'],
workingHours: json['workingHours'],
deleted: json['deleted'],
isApproved: json['isApproved'],
address: json['address'],
coverImages: json['coverImages'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["name"] = name;
map["description"] = description;
map["services"] = services;
map["albums"] = albums;
map["comments"] = comments;
map["imageURLArray"] = imageURLArray;
map["rating"] = rating;
map["shares"] = shares;
map["favorites"] = favorites;
map["category"] = category;
map["subcategory"] = subcategory;
map["workingHours"] = workingHours;
map["deleted"] = deleted;
map["isApproved"] = isApproved;
map["address"] = address;
map["coverImages"] = coverImages;
return map;
}
}
class PhoneNumber {
int phoneNumber;
String owner;
PhoneNumber({
this.phoneNumber,
this.owner
});
factory PhoneNumber.fromJson(Map<String, dynamic> json) {
return PhoneNumber(
phoneNumber: json['phoneNumber'],
owner: json['owner'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["phoneNumber"] = phoneNumber;
map["owner"] = owner;
return map;
}
}
class GPSCoordinante {
double longitude;
double latitude;
GPSCoordinante({
this.longitude,
this.latitude
});
factory GPSCoordinante.fromJson(Map<String, dynamic> json) {
return GPSCoordinante(
longitude: json['longitude'],
latitude: json['latitude'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["longitude"] = longitude;
map["latitude"] = latitude;
return map;
}
}
class Service {
String name;
String description;
Service({
this.name,
this.description
});
factory Service.fromJson(Map<String, dynamic> json) {
return Service(
name: json['name'],
description: json['description'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["name"] = name;
map["description"] = description;
return map;
}
}
class Album {
String name;
String nameEn;
List<Item> items;
bool isAutoConvertToUSDEnabled;
bool isAllItemsDeliveryEnabled;
Album({
this.name,
this.nameEn,
this.items,
this.isAutoConvertToUSDEnabled,
this.isAllItemsDeliveryEnabled
});
factory Album.fromJson(Map<String, dynamic> json) {
List itemsJsonList = json['items'] as List;
List<Item> parsedItems = itemsJsonList.map((value) => Item.fromJson(value)).toList();
return Album(
name: json['name'],
nameEn: json['nameEn'],
items: parsedItems,
isAutoConvertToUSDEnabled: json['isAutoConvertToUSDEnabled'],
isAllItemsDeliveryEnabled: json['isAllItemsDeliveryEnabled']
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["name"] = name;
map["nameEn"] = nameEn;
map["items"] = items;
map["isAutoConvertToUSDEnabled"] = isAutoConvertToUSDEnabled;
map["isAllItemsDeliveryEnabled"] = isAllItemsDeliveryEnabled;
return map;
}
}
class Item{
List<String> imageVariants;
String name;
String nameEn;
bool isAutoConvertNameToEnglishEnabled;
List<String> tags;
int priceIQD;
double priceUSD;
String description;
bool isDeliveryAvailable;
bool isDinarAutomaticallyConvertedToDollar;
int itemIndex;
int albumIndex;
bool isDeleted;
Item({
this.imageVariants,
this.name,
this.nameEn,
this.isAutoConvertNameToEnglishEnabled,
this.tags,
this.priceIQD,
this.priceUSD,
this.description,
this.isDeliveryAvailable,
this.isDinarAutomaticallyConvertedToDollar,
this.itemIndex,
this.albumIndex,
this.isDeleted
});
factory Item.fromJson(Map<String, dynamic> json) {
return Item(
imageVariants: json['imageVariants'],
name: json['name'],
nameEn: json['nameEn'],
isAutoConvertNameToEnglishEnabled: json['isAutoConvertNameToEnglishEnabled'],
tags: json['tags'],
priceIQD: json['priceIQD'],
priceUSD: json['priceUSD'],
description: json['description'],
isDeliveryAvailable: json['isDeliveryAvailable'],
isDinarAutomaticallyConvertedToDollar: json['isDinarAutomaticallyConvertedToDollar'],
albumIndex: json['albumIndex'],
itemIndex: json['itemIndex'],
isDeleted: json['isDeleted'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["imageVariants"] = imageVariants;
map["name"] = name;
map["nameEn"] = nameEn;
map["isAutoConvertNameToEnglishEnabled"] = isAutoConvertNameToEnglishEnabled;
map["tags"] = tags;
map["priceIQD"] = priceIQD;
map["priceUSD"] = priceUSD;
map["description"] = description;
map["isDeliveryAvailable"] = isDeliveryAvailable;
map["isDinarAutomaticallyConvertedToDollar"] = isDinarAutomaticallyConvertedToDollar;
map["albumIndex"] = albumIndex;
map["itemIndex"] = itemIndex;
map["isDeleted"] = isDeleted;
return map;
}
}
class Comment{
String user;
String userId;
String text;
DateTime dateTime;
Comment({
this.user,
this.userId,
this.text,
this.dateTime
});
factory Comment.fromJson(Map<String, dynamic> json) {
return Comment(
user: json['user'],
userId: json['userId'],
text: json['text'],
dateTime: json['dateTime'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["user"] = user;
map["userId"] = userId;
map["text"] = text;
map["dateTime"] = dateTime;
return map;
}
}
class WorkingHour{
String openingHour;
String openingHourAmOrPm;
String closingHour;
String closingHourAmOrPm;
WorkingHour({
this.openingHour,
this.openingHourAmOrPm,
this.closingHour,
this.closingHourAmOrPm
});
factory WorkingHour.fromJson(Map<String, dynamic> json) {
return WorkingHour(
openingHour: json['openingHour'],
openingHourAmOrPm: json['openingHourAmOrPm'],
closingHour: json['closingHour'],
closingHourAmOrPm: json['closingHourAmOrPm'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["openingHour"] = openingHour;
map["openingHourAmOrPm"] = openingHourAmOrPm;
map["closingHour"] = closingHour;
map["closingHourAmOrPm"] = closingHourAmOrPm;
return map;
}
}
class SocialMedia {
String facebook ;
String instagram;
String youTube;
String snapChat;
String twitter;
String googlePlus;
String pinterest;
SocialMedia({
this.facebook,
this.instagram,
this.youTube,
this.snapChat,
this.twitter,
this.googlePlus,
this.pinterest
});
factory SocialMedia.fromJson(Map<String, dynamic> json) {
return SocialMedia(
facebook: json['facebook'],
instagram: json['instagram'],
youTube: json['youTube'],
snapChat: json['snapChat'],
twitter: json['twitter'],
googlePlus: json['googlePlus'],
pinterest: json['pinterest'],
);
}
Map toMap() {
var map = new Map<String, dynamic>();
map["facebook"] = facebook;
map["instagram"] = instagram;
map["youTube"] = youTube;
map["twitter"] = twitter;
map["snapChat"] = snapChat;
map["googlePlus"] = googlePlus;
map["pinterest"] = pinterest;
return map;
}
}
I am trying to send this Place object to the server:
Place place = Place(
name: 'some name',
description: 'some description',
phoneNumbers: [PhoneNumber(phoneNumber: 125252525, owner: 'ali')],
tags: ['some tag 2', 'some tag 2'],
gpsCoordinates: GPSCoordinante(latitude: 11332, longitude: 13415),
services: [Service(name: 'some service', description: 'some description')],
albums: [
Album(
name: 'some album name',
nameEn: 'someEN name',
items: [
Item(
name: 'some item name',
imageVariants: ['a;lgjlagj', 'ag;ja;gj;ag'],
nameEn: 'some en name',
isAutoConvertNameToEnglishEnabled: true,
tags: ['aggagag'],
priceIQD: 32425,
priceUSD: 252525,
description: 'agkgl;aj g;g ja;g ',
isDeliveryAvailable: true,
isDinarAutomaticallyConvertedToDollar: true,
itemIndex: 1,
albumIndex: 2)
],
isAutoConvertToUSDEnabled: true,
isAllItemsDeliveryEnabled: false)
],
socialMedia: SocialMedia(facebook: 'facebook url'),
comments: [
Comment(
user: 'some user',
userId: '324-2-5-25',
text: 'some comment',
dateTime: DateTime.now())
],
imageURLArray: ['some url'],
rating: 0,
shares: 0,
favorites: 0,
views: 0,
category: 'sdagagasga;lgjaa;lgj',
subcategory: 'as;glgjasl;gjas;lgkj',
workingHours: WorkingHour(
openingHour: '11', openingHourAmOrPm: 'am', closingHour: '11:00'),
deleted: false,
distanceToUser: 225252,
isApproved: false,
address: "some adress",
coverImages: ['image one', 'image 2']);
I keep getting errors when using json.encode(place)
;
or json.encode(place.toMap())
like this
converting object to an encodable object failed: Instance of 'Service'
How can make this work ?