我无法从数据库填充DropDown。
Future<List<Map<String, dynamic>>> getProjectsMapList() async {
Database db = await this.database;
var result = await db.query(projectTable);
return result;
}
Future<List<Project>> getProjectsList() async {
var projectsMapList = await getProjectsMapList(); // Get 'Map List' from database
int count = projectsMapList.length; // Count the number of map entries in db table
List<Project> projectList = List<Project>();
// For loop to create a 'categories List' from a 'Map List'
for (int i = 0; i < count; i++) {
projectList.add(Project.fromMapObject(projectsMapList[i]));
}
return projectList;
}
我在其中创建了DropDown的项目类,但下拉菜单中没有显示任何值。
Project projectModel;
List<Project> newList = <Project> [];
@override
void initState() {
super.initState();
_getProjectList();
}
void _getProjectList() async {
final List<Project> _listPj = await databaseHelper.getProjectsList();
setState(() {
newList = _listPj;
});
}
DropDown
DropdownButtonFormField<Project>(
hint: Text('Project'),
value: projectModel,
onChanged: (Project value){
setState(() {
projectModel = value;
});
},
items: newList.map((project) => DropdownMenuItem<Project>(
child: Text(projectModel.title),
value: project,
)
).toList()
),
模型类
int _id;
String _title;
String _date;
Project(this._title, this._date);
int get id => _id;
String get title => _title;
String get date => _date;
set id(int value) {
this._id = value;
}
set title(String newTitle) {
if (newTitle.length <= 255) {
this._title = newTitle;
}
}
set date(String newDate) {
this._date = newDate;
}
// Convert a Note object into a Map object
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
if (id != null) {
map['id'] = _id;
}
map['title'] = _title;
map['date'] = _date;
return map;
}
// Extract a Note object from a Map object
Project.fromMapObject(Map<String, dynamic> map) {
this._id = map['id'];
this._title = map['title'];
this._date = map['date'];
}
}
这是代码,但是我不明白为什么下拉列表中没有显示数据?有人可以帮助我吗?我收到NoSuchMethodError:在NULL上调用了吸气剂“ title”,但我不知道我在哪里做过miatake?
答案 0 :(得分:1)
请将 child: Text(projectModel.title)
改为 child: Text(project.title)
items: newList.map((project) => DropdownMenuItem<Project>(
child: Text(project.title),
value: project,
)
).toList()