【发布时间】:2019-12-23 08:57:24
【问题描述】:
我使用 dart 和返回小部件的颤振框架创建了这个函数:
Widget adjustImage(String weatherPicture, int day) {
if (weatherPicture == 'images/01n.png' || weatherPicture == 'images/13d.png' || weatherPicture == 'images/13n.png') {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${giveWeekday(convertEpochToDate(forecastDay[day]))}',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
fontSize: 17.0,
),
),
Image.asset(
weatherPicture,
width: 15.0,
height: 15.0,
),
Text(
'${forecastTemperature[day]}°',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
),
),
],
);
} else if (weatherPicture == 'images/01d.png') {
//sun
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${giveWeekday(convertEpochToDate(forecastDay[day]))}',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
fontSize: 17.0,
),
),
Image.asset(
weatherPicture,
width: 30.0,
height: 30.0,
),
Text(
'${forecastTemperature[day]}°',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
),
),
],
);
} else if (weatherPicture == 'images/02d.png' ||
weatherPicture == 'images/02n.png') {
Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${giveWeekday(convertEpochToDate(forecastDay[day]))}',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
fontSize: 17.0,
),
),
Image.asset(
weatherPicture,
width: 30.0,
height: 30.0,
),
Text(
'${forecastTemperature[day]}°',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
),
),
],
);
} else {
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
'${giveWeekday(convertEpochToDate(forecastDay[day]))}',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.w600,
fontSize: 17.0,
),
),
Image.asset(
weatherPicture,
width: 30.0,
height: 30.0,
),
Text(
'${forecastTemperature[day]}°',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0,
),
),
],
);
}
}
但是,该函数声称它的返回类型为“Widget”,但没有以 return 语句结束。我不确定为什么会这样,因为我专门用 else 条件结束了它,只是为了确保它总是返回一个小部件。此外,在某些情况下,它会使我的应用程序崩溃并告诉我断言失败。
我不确定是什么原因造成的。
【问题讨论】: