Created with <3 with dartpad.dev.
Created
October 3, 2023 18:39
-
-
Save MelvinRB27/65e365d5d4cb7d225f4f62301703a50b to your computer and use it in GitHub Desktop.
flying-villa-2200
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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