【发布时间】:2020-05-04 06:55:43
【问题描述】:
我正在尝试制作一个 dropdownButtonFormField,其中包含来自 sqflite 数据库的对象值列表。我到了会显示列表项的地步,但是当我单击其中一个时,它会喊出错误
class Test extends StatefulWidget {
@override
_TestState createState() => _TestState();
}
class _TestState extends State<Test> {
Section currentSection;
@override
Widget build(BuildContext context) {
final sectionsProvider = Provider.of<SectionsProvider>(context);
return Scaffold(
body: Container(
padding: EdgeInsets.all(15),
child: FutureBuilder<List<Section>>(
future: sectionsProvider.getSections(),
builder: (BuildContext context,AsyncSnapshot<List<Section>> snapshot){
if(!snapshot.hasData){
return Text('Loading...');
}else{
return DropdownButtonFormField<Section>(
//decoration: inputDecoration.copyWith(hintText: currentSection.title),
//value: currentSection,
items: snapshot.data.map((section){
return DropdownMenuItem<Section>(
value: section,
child: Row(
children: [
Icon(
Icons.brightness_1,
size: 15,
color: Color(section.color),
),
SizedBox(width: 20,),
Text(section.title),
],
),
);
},
).toList(),
isExpanded: false,
isDense: true,
onChanged: (value){
setState(() {
currentSection = value;
});
},
);
}
},
),
),
);
}
}
【问题讨论】: