The getter isn't defined for the class 'Map<>' List getter for a Map
Solution 1
The error is because you are most probably trying to get the list directly from the class and not from an instance of the class, so making the getter static will solve your problem (also it is better be static because it already only access static values)
also, you had an error were you forgot to initialize the list
enum Course {
math,
english,
}
class CourseData {
const CourseData({@required this.courseName, @required this.schoolCourse});
final String courseName;
final bool schoolCourse;
static const Map<Course, CourseData> allCourses = {
Course.math: CourseData(courseName: 'math', schoolCourse: true),
Course.english: CourseData(courseName: 'english', schoolCourse: true),
};
static List<CourseData> get list {
List<CourseData> list = List<CourseData>();
allCourses.forEach((k, v) => list.add(v));
return list;
}
@override
String toString() {
return 'CourseData{courseName: $courseName, schoolCourse: $schoolCourse}';
}
}
Solution 2
You are missing to initialise list.
Change following line:
List<CourseData> list;
with Following one:
List<CourseData> list = [];
maxpill
Github: https://github.com/maxpill LinkedIn: https://pl.linkedin.com/in/maksymilian-pilżys-b810b8175
Updated on December 19, 2022Comments
-
maxpill over 1 year
import 'package:meta/meta.dart'; enum Course { math, english, } class CourseData { const CourseData({@required this.courseName, @required this.schoolCourse}); final String courseName; final bool schoolCourse; static const Map<Course, CourseData> allCourses = { Course.math: CourseData(courseName: 'math', schoolCourse: true), Course.english: CourseData(courseName: 'english', schoolCourse: true), }; List<CourseData> get list { List<CourseData> list; allCourses.forEach((k, v) => list.add(v)); return list; } }
Hello, I have a class as shown above. I have a Map and i want to make a List from it. So i created a getter but it doesnt work because when i try to
print(courses.list);
i get an error"
The getter 'list' isn't defined for the class 'Map<Course, CourseData>'.
"-
Haidar about 4 yearscan you show the code where you are printing the list
-