Skip to content

Instantly share code, notes, and snippets.

@MelvinRB27
Created October 3, 2023 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelvinRB27/65e365d5d4cb7d225f4f62301703a50b to your computer and use it in GitHub Desktop.
Save MelvinRB27/65e365d5d4cb7d225f4f62301703a50b to your computer and use it in GitHub Desktop.
flying-villa-2200
void main() {
final Map<String, dynamic> responseHttp = {
'heroes': [
{'name': 'Spider Man', 'power': 'Trepar', 'isAlive': true},
{
'name': 'Iron Man',
'power': 'Traje de alta tecnología',
'isAlive': false
},
{
'name': 'Captain America',
'power': 'Súper fuerza y agilidad',
'isAlive': true
},
{
'name': 'Thor',
'power': 'Control del trueno y el martillo Mjolnir',
'isAlive': true
},
{
'name': 'Black Widow',
'power': 'Habilidades de espionaje y combate',
'isAlive': false
}
]
};
List<Hero> heroesList = (responseHttp['heroes'] as List<dynamic>)
.map((heroData) => Hero.heroToJson(heroData))
.toList();
// Ahora tienes una lista de objetos Hero basada en los datos del mapa
for (var hero in heroesList) {
print(hero);
}
/* final spiderMan = Hero.heroToJson(responseHttp);
print(spiderMan);*/
/*final he = Hero('Melvin', 'Flash', true);
print(he);*/
}
class Hero {
String name;
String power;
bool isAlive;
Hero(this.name, this.power, this.isAlive);
Hero.heroToJson(Map<String, dynamic> json)
: name = json['name'] ?? 'not name found',
power = json['power'] ?? 'not power found',
isAlive = json['isAlive'] ?? 'not isAlive found';
@override
String toString() {
return 'Name: $name, Power: $power, isAlive: ${isAlive ? 'YES!' : 'NO'}';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment