[英]Flutter :: change value in initialstate in statefull widget from future list
If I have statefull widget with initial satate variable called value,Like this:如果我有状态小部件,初始状态变量称为值,就像这样:
@override
void initState() {
thisDayActivity = dataBase.getDetails(widget.courseId, widget.actId);
value = 0.0;
super.initState();
}
thisDayActivity is future list comming from sqflite database, I want check if the list is empty the value variable equal to 0,else value equale some data in future list. thisDayActivity 是来自 sqflite 数据库的未来列表,我想检查列表是否为空,值变量等于 0,否则值等于未来列表中的一些数据。
I tride this but don't work:我试过这个但不起作用:
@override
void initState() {
thisDayActivity = dataBase.getDetails(widget.courseId, widget.actId);
if (thisDayActivity == []) {
value = 0.0;
} else {
value = thisDayActivity[0]['digree'].toDouble();
}
super.initState();
}
How can I solve this?我该如何解决这个问题?
your method is not working since you are reading a value from a future function, what you need to do is to use the then
method to achieve your goal like this:您的方法不起作用,因为您正在读取未来 function 的值,您需要做的是使用then
方法来实现您的目标,如下所示:
@override
void initState() {
dataBase.getDetails(widget.courseId, widget.actId)
.then((thisDayActivity) {
if (thisDayActivity == []) {
value = 0.0;
} else {
value = thisDayActivity[0]['digree'].toDouble();
}
});
super.initState();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.