Skip to content

Instantly share code, notes, and snippets.

@lordvidex
Last active February 18, 2020 18:05
Show Gist options
  • Save lordvidex/b338f2a46bcb361de5e05c5ea6b8c74f to your computer and use it in GitHub Desktop.
Save lordvidex/b338f2a46bcb361de5e05c5ea6b8c74f to your computer and use it in GitHub Desktop.
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