【发布时间】:2021-06-06 17:23:19
【问题描述】:
警告:
This function has a return type of 'int', but doesn't end with a return statement. Try adding a return statement, or changing the return type to 'void'.
我在这段代码中收到警告:
int _duration = 0;
int getDuration(String date) {
//print("Date $date");
//load the data from firebase and add to the list
fb.reference()
..child(cuser.uid)
.child('Seizure_history')
.child(date)
.once()
.then((DataSnapshot snapshot) {
var data = snapshot.value;
list = List();
if (data != null) {
data.forEach((key, value) {
EventList seizure_history = new EventList(
seiz_duration: value['duration'],
dateTime: value['datetime'],
key: key,
);
list.add(seizure_history);
setState(() {
_duration = int.parse(list[list.length - 1].seiz_duration);
});
print("Duration $_duration");
return _duration;
});
}
});
}
我也返回了持续时间,但它仍然显示警告,请帮助我哪里出错了?它在控制台上正确打印持续时间但没有返回。
我想在firebase中找到它后返回持续时间,但它仍然显示为零,这意味着没有返回值,为什么会发生。我是 Flutter 新手,还在学习中。
更新:
这个函数在initState()内部调用:
var series;
var data;
//Charts
getCharts()
{
data = [
addcharts(sevenDay.substring(0, 2), getDuration(seven),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(sixDay.substring(0, 2), getDuration(six),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(fiveDay.substring(0, 2),getDuration(five),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(fourDay.substring(0, 2), getDuration(four),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(threeDay.substring(0, 2), getDuration(three),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(twoDay.substring(0, 2), getDuration(two),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
addcharts(oneDay.substring(0, 2), getDuration(one),
charts.ColorUtil.fromDartColor(const Color(0xffe8e5af))),
];
series = [
charts.Series(
domainFn: (addcharts addcharts, _) => addcharts.weeks,
measureFn: (addcharts addcharts, _) => addcharts.duration,
colorFn: (addcharts addcharts, _) => addcharts.barColor,
id: 'addcharts',
data: data,
),
];
}
我怎样才能在里面使用future Builder?如果我使用 FutureBuilder,这就是我卡住的地方
Expanded(
child: charts.BarChart(
series,
animationDuration: Duration(microseconds: 2000),
),
)
【问题讨论】:
-
您将在
if块内返回Duration实例,该实例又位于快照 (then()) 内,然后在forEach循环内,最好使用一些默认持续时间,在 if 块中更新它并返回相同的变量或使用回调。 -
@DarShan 不要只选择变量名。