I am trying to develope an travel app. I have four model class whic are Place, Place_info , County and Category. County has a property that 'places'. This property's type is List. My Place object has a property that 'county'. This property's type is County. When I want to create a Place object it goes to County class for creat Place's County object while it is creating a county object it goes to Place object to create County's Places list. İt is getting into a dead-end loop. I can't find any solution for this problem. Can you help me?
Here my models:
class Place {
final String id;
final String name;
final String image;
final List<County?> county;
final List<PlaceInfo> placeInfos;
final String firstInfo;
final List<Category?> categories;
Place({
required this.id,
required this.name,
required this.image,
required this.county,
required this.placeInfos,
required this.firstInfo,
required this.categories,
});
factory Place.fromMap(Map<String, dynamic> place) => Place(
id: place["id"],
name: place["name"],
image: place["image"],
county: List.from(Repository().countiesJSON.map((countyJSON) {
if (countyJSON.containsValue(place["countyId"])) {
return County.fromMap(countyJSON);
}
}).toList()),
placeInfos: List<PlaceInfo>.from(
place["information"].map(
(item) => PlaceInfo.fromMap(item),
),
),
firstInfo: place["firstInfo"],
categories: List.from(place["categoriesId"]
.map(
(categoyId) => Repository()
.categoriesJSON
.map((categoryJSON) {
if (categoryJSON.containsValue(categoyId)) {
return Category.fromMap(categoryJSON);
}
})
.iterator
.current,
)
.toList()));
}
class PlaceInfo {
final String infoTitle;
final String info;
PlaceInfo({
required this.infoTitle,
required this.info,
});
factory PlaceInfo.fromMap(Map<String, dynamic> placeInfo) => PlaceInfo(
infoTitle: placeInfo["infoTile"],
info: placeInfo["info"],
);
}
class Category {
String id;
final String name;
final String image;
final List<Place?> places;
Category({
required this.id,
required this.name,
required this.image,
required this.places,
});
factory Category.fromMap(Map<String, dynamic> category) => Category(
id: category["id"],
name: category["name"],
image: category["image"],
places: List.from(category["placesId"]
.map(
(placeId) => Repository()
.placesJSON
.map((placeJSON) {
if (placeJSON.containsValue(placeId)) {
return Place.fromMap(placeId);
}
})
.iterator
.current,
)
.toList()),
);
}
class County {
final String id;
final String name;
final String image;
final List<Place?> places;
County({
required this.id,
required this.name,
required this.image,
required this.places,
});
factory County.fromMap(Map<String, dynamic> county) => County(
id: county["id"],
name: county["name"],
image: county["image"],
places: List.from(county["placesId"]
.map((placeId) => Repository().placesJSON.map((placeJSON) {
if (placeJSON.containsValue(placeId)) {
return Place.fromMap(placeJSON);
}
}).toList())
.toList()),
);
}
Here my demo data:
var placesData = [
{
"id": "d",
"name": "Dolmabahçe Sarayı",
"image": "assets/images/dolmabahçe.jpg",
"countyId": "b",
"firstInfo": "Dolmabahçe Sarayı firstInfo",
"information": [
{"infoTile": "bilgi 1", "info": "bilgi 1 açıklama"},
{"infoTile": "bilgi 2", "info": "bilgi 2 açıklama"},
{"infoTile": "bilgi 3", "info": "bilgi 3 açıklama"},
],
"categoriesId": [
"m",
"s",
],
},
{
"id": "a",
"name": "Ayasofya",
"image": "assets/images/ayasofya.webp",
"countyId": "s",
"firstInfo": "Ayasofya firstInfo",
"information": [
{"infoTile": "bilgi 1", "info": "bilgi 1 açıklama"},
{"infoTile": "bilgi 2", "info": "bilgi 2 açıklama"},
{"infoTile": "bilgi 3", "info": "bilgi 3 açıklama"},
],
"categoriesId": [
"c",
],
},
{
"id": "y",
"name": "Yerebatan Sarnıcı",
"image": "assets/images/yerebatan.webp",
"countyId": "s",
"firstInfo": "Yerebatan Sarnıcı firstInfo",
"information": [
{"infoTile": "bilgi 1", "info": "bilgi 1 açıklama"},
{"infoTile": "bilgi 2", "info": "bilgi 2 açıklama"},
{"infoTile": "bilgi 3", "info": "bilgi 3 açıklama"},
],
"categoriesId": [
"m",
],
},
];
var categoriesData = [
{
"id": "m",
"name": "Müze",
"image": "assets/images/müze.webp",
"placesId": [
"d",
"y",
],
},
{
"id": "r",
"name": "Restorant",
"image": "assets/images/restorant.webp",
"placesId": [],
},
{
"id": "e",
"name": "Eğlence",
"image": "assets/images/eğlence.webp",
"placesId": [],
},
{
"id": "s",
"name": "Saray",
"image": "assets/images/saray.webp",
"placesId": [
"d",
],
},
{
"id": "a",
"name": "Alışveriş",
"image": "assets/images/alışveriş.webp",
"placesId": [],
},
{
"id": "c",
"name": "Cami",
"image": "assets/images/cami.webp",
"placesId": [
"a",
],
},
{
"id": "d",
"name": "Doğa",
"image": "assets/images/doğa.webp",
"placesId": [],
},
];
var countiesData = [
{
"id": "b",
"name": "Beşiktaş",
"image": "assets/images/beşiktaş.jpg",
"placesId": [
"d",
],
},
{
"id": "s",
"name": "Sultanahmet",
"image": "assets/images/Sultanahmet.jpg",
"placesId": [
"a",
"y",
]
}
];