Reputation: 49
I want to convert the List to Map and get only 'CourseName', how can I do this?
Here is the Model
`class Course {
final int courseCategoryId;
final int courseSubCategoryId;
final int courseId;
final Color onlineTrueColor;
final String coverImgCourse;
final String courseName;
final String courseTeaserText;
final int courseTutorId;
final String courseGrads;
final int coursePrice;
final String courseCashBack;
final bool isCourseOnline;
final String courseDescription;
Course ( {
this.courseDescription,
this.courseCategoryId,
this.courseSubCategoryId,
this.courseId,
this.onlineTrueColor,
this.coverImgCourse,
this.courseName,
this.courseTeaserText,
this.courseTutorId,
this.courseGrads,
this.coursePrice,
this.courseCashBack,
this.isCourseOnline,
});
} `
(I need to convert it to map, cuz I need this for my dropdown list, it only works with map, if you think that there's could be another solution please share it with me)
DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
isDense: true,
hint: Text('Выберите категорию'),
value: _selected,
onChanged: (String newValue) {
setState(() {
_selected = newValue;
});
},
items: _course.map((Map map) {
return new DropdownMenuItem<String>(
value: map["id"].toString(),
child: Row(
children: <Widget> [
Container(
margin: EdgeInsets.only(left: 10),
child: Text(map['courseName']),
)
],
));
}).toList(),
),
),)
Upvotes: 1
Views: 1130
Reputation: 116
You do not need to convert it to a map. Using a list is fine. If you notice, in the end of your items, after you build them the .toList() method is called. In the end you are creating a list of DropdownMenuItem.
Solution: You can use the List.generate() method and build all your items directly from a list.
This is how your code should look like:
items: List.generate(yourList.length, (index) =>
return DropdownMenuItem<String>(
value: yourList[index].id.toString(),
child: Row(
children: <Widget> [
Container(
margin: EdgeInsets.only(left: 10),
child: Text(yourList[index].courseName),
)
],
)
);
),
Upvotes: 1