RenderFlex在右侧溢出了265个像素

时间:2020-01-27 19:17:44

标签: flutter openweathermap

我正在尝试从网络中获取天气数据,并在openweathermap上仅显示5days / 3hours API的五个索引。当我第一次运行该应用程序时,它运行良好,但是当我单击Iconbutton按钮并再次调用updateUI时,它将为我提供所有数据,而不是五个。这是我的代码:

List<String timeStringData = [];
List<String> hourlyTemp =[];
void updateUI(dynamic weatherData, dynamic hourlyData) {
  setState(() {
    temperature = weatherData['main']['temp'].toInt();
    description = weatherData['weather'][0]['description'];
    cityName = weatherData['name'];
    for (int i = 1; i < 6; i++) {
      timeStringData.add(hourlyData['list'][i]['dt_txt']);
      hourlyTemp.add(hourlyData['list'][i]['main']['temp']);
      parsing();
    }
  });
}

Row parsing() {
  List<HourInfo> hourTexts = [];
  for (int i = 0; i <= timeStringData.length - 1; i++) {
    var parsedDate = DateTime.parse(timeStringData[i]);
    String formatted = formatting(parsedDate);
    String onlyTemp = hourlyTemp[i].toStringAsFixed(0);
    setState(() {
      hourTexts.add(
        HourInfo(
          onlyHour: formatted,
          onlyTemp: onlyTemp,
        ),
      );
    });
  }
  return Row(
    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: hourTexts,
  );
}

构建方法:

IconButton(
  icon: Icon(
    Icons.near_me,
    color: Colors.white,
    size: 35.0,
  ),
  onPressed: () async {
    var weatherData = await weather.getLocationWeather();
    var hourlyData = await weather.get3HoursWeather();
    updateUI(weatherData, hourlyData);
  },
),

Container(
  width: double.infinity,
  height: 100.0,
  child: parsing(),
),

1 个答案:

答案 0 :(得分:0)

我认为您正在将准备数据与构建小部件树混合在一起。

只需从parsing();方法中删除updateUI()调用即可。 parsing();将在构建树时由库调用。