Last active
February 18, 2020 18:05
-
-
Save lordvidex/b338f2a46bcb361de5e05c5ea6b8c74f to your computer and use it in GitHub Desktop.
Stack Overflow answer to Question https://stackoverflow.com/questions/60286533/why-i-cant-refer-to-a-key-in-an-objects-another-key
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
var object = { | |
"allStudents": [ | |
{ | |
"name": "John", | |
"hobbies": { | |
"sport": "Football", | |
"music": "Piano" | |
} , | |
}, | |
{ | |
"name": "Julyo", | |
"hobbies": { | |
"sport": "Vollyball", | |
"music": "Guitar" | |
} , | |
}, | |
], | |
}; | |
class Student { | |
String name; | |
dynamic hobbies; | |
Student({this.name, this.hobbies}); | |
} | |
class Hobbies { | |
String sport; | |
String music; | |
Hobbies({this.sport, this.music}); | |
} | |
List<Student> studentsList = []; | |
List getStudentsList () { | |
//new Line here - ignore the unnecessary cast warning | |
studentsList = (object["allStudents"] as List<Map<String,dynamic>>).map((stu)=> | |
Student( | |
name: stu["name"] , | |
hobbies: Hobbies( | |
sport: stu['hobbies']["sport"] , | |
music:stu['hobbies']["music"], | |
) , | |
) | |
).toList(); | |
return studentsList; | |
} | |
void main(){ | |
print(getStudentsList()[0].hobbies.sport); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment