Нужна помощь с декодированием Json flutter
У меня есть несколько классов с toJson и fromJson. Проблема в том, что они содержаться в Folder.directoryChildrens и мне нужно в Folder.fromJson каждый класс как-то декодировать, чтобы все заработало. Есть варианты как это можно сделать?
class Folder {
final String directoryName;
final List directoryChildrens;
Folder({
required this.directoryName,
required this.directoryChildrens,
});
Folder.fromJson(Map<String, dynamic> json)
: directoryName = json['directoryName'],
directoryChildrens = json['directoryChildrens'];
Map<String, dynamic> toJson() => {
'directoryName': directoryName,
'directoryChildrens':
directoryChildrens.map((e) => e.toJson()).toList(),
};
}
// Пример класса, который добавляется в Folder.directoryChildrens
class Chat {
AllType type;
String? name;
bool animate;
bool dublicated;
bool link;
bool pinned;
List? messages;
List? favorites;
String pathToImage;
Chat(
{this.type = AllType.chat,
this.name,
this.animate = false,
this.pinned = false,
this.dublicated = false,
this.link = false,
this.favorites,
this.pathToImage = '',
this.messages});
Chat.fromJson(Map<String, dynamic> json)
: type = AllType.values.elementAt(json['type']),
name = json['name'],
link = json['link'],
pinned = json['pinned'],
dublicated = json['dublicated'],
animate = json['animate'],
messages = json['messages'],
pathToImage = json['pathToImage'],
favorites = json['favorites'];
Map<String, dynamic> toJson() {
return {
'type': type.index,
'name': name,
'link': link,
'pinned': pinned,
'dublicated': dublicated,
'animate': animate,
'messages': messages!.map((e) => e.toJson()).toList(),
'pathToImage': pathToImage,
'favorites': favorites
};
}
}
// еще один класс, который добавляется в Folder.directoryChildrens
class StorageFile {
AllType type;
String? name;
String? data;
List? history;
bool dublicated;
bool link;
bool animate;
bool pinned;
String pathToImage;
StorageFile(
{this.type = AllType.storageFile,
this.name,
this.data,
this.link = false,
this.dublicated = false,
this.pinned = false,
this.pathToImage = '',
this.history,
this.animate = false});
StorageFile.fromJson(Map<String, dynamic> json)
: type = AllType.values.elementAt(json['type']),
name = json['name'],
link = json['link'],
dublicated = json['dublicated'],
pinned = json['pinned'],
data = json['data'],
history = json['history'],
pathToImage = json['pathToImage'],
animate = json['animate'];
Map<String, dynamic> toJson() => {
'type': type.index,
'name': name,
'link': link,
'dublicated': dublicated,
'pinned': pinned,
'data': data,
'history': history,
'animate': animate,
'pathToImage': pathToImage
};
}
Ответы (1 шт):
Автор решения: ddo5
→ Ссылка
Если я правильно понял, AllType указывает на тип конкретной модели. В таком случае следующее решение должно помочь:
directoryChildrens =
(json['directoryChildrens'] as List<dynamic>).map<dynamic>((dynamic e) {
switch(AllType.values.elementAt(e['type'])) {
case AllType.chat:
return Chat.fromJson(e as Map<String, dynamic>);
case AllType.storageFile:
return StorageFile.fromJson(e as Map<String, dynamic>);
}
}).toList();
Если же нет, то вам определенно нужно получать тип с сервера.