我已经建立了一个自定义列表。现在我包含一个复选框,如果我要选中或未选中,则会引发以下错误:'setState() called in constructor'
class Lists extends StatefulWidget{
@override
_List createState() => _List();
}
class _List extends State<Lists> {
bool checkedvalue = true;
@override
Widget build(BuildContext context) {
return futureBuilder();
}
Widget futureBuilder(){
var futureBuilder = new FutureBuilder(
future: rest.fetchPost(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
return new Text('loading...');
default:
if (snapshot.hasError)
return new Text('Error: ${snapshot.error}');
else
return listBuilder(context, snapshot);
}
}
);
return new Scaffold(
body: futureBuilder,
);
}
Widget listBuilder(BuildContext context, AsyncSnapshot snapshot) {
List<rest.Status> values = snapshot.data;
if (values == null || values.length == 0){
return null;
}
int items = values.length;
return ListView.builder(
itemCount: items,
itemBuilder: (BuildContext context, int index) {
String statusText;
Image image ;
Uint8List bytes;
if(statusList.globalStatus != null){
for(int i=0;i< statusList.globalStatus.length; i++){
if(values[index].statusID == statusList.globalStatus[i].id){
if(statusList.globalStatus[i].kurzform != null){
statusText = statusList.globalStatus[i].kurzform;
}else{
statusText = statusList.globalStatus[i].kurzform;
}
if (statusList.globalStatus[i].icon != null){
bytes = base64Decode(statusList.globalStatus[i].icon);
image = new Image.memory(bytes) ;
}
}
if(image== null || statusText == null){
statusText= 'Kein Status';
image= new Image.asset('assets/null.png');
}
}
}
return new Container(
decoration: new BoxDecoration(
border: Border(top: BorderSide(
color: Colors.black26,
width: 1
)
)
),
child:Column(
children: <Widget>[
CustomListItemTwo(
statusText: statusText,
status:image,
materialNR: values[index].uArtText,
material: values[index].untersuchungsMaterialName,
probenArt: values[index].probenart,
eingansdatum: values[index].eingangsdatumText,
patient: values[index].vorname + ' ' + values[index].nachname ,
geburtsdatum: values[index].geburtstagText ,
),
Checkbox(
value: checkedvalue ,
onChanged: (bool newValue) =>
setState(() {
checkedvalue = newValue;
})
),
]
),
);
}
);
}
}
我/颤振(5067):══╡手势发现异常╞════════════════════════════════════════════ ════════════════════════════════ I/flutter(5067):处理手势时抛出以下断言:I /flutter ( 5067): setState() 在构造函数中调用: _List#9044e(lifecycle state: created, no widget, notmounted) I/flutter ( 5067): 当你在一个状态对象上为一个小部件调用 setState() 时会发生这种情况尚未插入到 I/flutter ( 5067): 小部件树。没有必要在构造函数中调用 setState(),因为状态是 I/flutter ( 5067):在最初创建时已经假定它是脏的。