【问题标题】:Error: Function Claims that it does not Return Anything错误:函数声称它不返回任何东西
【发布时间】: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 条件结束了它,只是为了确保它总是返回一个小部件。此外,在某些情况下,它会使我的应用程序崩溃并告诉我断言失败。

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

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    在您的第二个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,
              ),
            ),
          ],
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2019-09-14
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      相关资源
      最近更新 更多