错误:函数声称它不返回任何东西

时间:2019-08-17 08:44:09

标签: flutter dart

我已经使用dart和返回窗口小部件的flutter框架创建了此函数:

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语句结尾。我不太确定为什么会这样,因为我已经以其他条件专门结束了它,只是为了确保它始终返回小部件。此外,在某些情况下,它使我的应用程序崩溃并告诉我断言失败。

我不确定是什么原因造成的。

1 个答案:

答案 0 :(得分:0)

在您的第二个else if中,您忘记使用return

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') {
    return Column( // you forgot to use return here
      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,
          ),
        ),
      ],
    );
  }
}
相关问题