我已经尝试解决这个错误了好几个小时,但似乎无法修复它。我的应用程序从快速加载屏幕转到主屏幕,但是在此期间,屏幕闪烁红色并出现以下错误:

我设法将其隔离到代码中,这是我显示预测信息的底部容器。

                      Container(
                        height: 150,
                        margin: EdgeInsets.fromLTRB(30, 0, 0, 30),
                        decoration: BoxDecoration(
                          color: Color(0xFF2F3148),
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(30.0),
                            bottomLeft: Radius.circular(30.0),
                          ),
                        ),
                        child: ListView(
                          scrollDirection: Axis.horizontal,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.stretch,
                              children: selectedWeather(),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
SelectedWeather方法是这样的:
 ///Returns the hourly or daily weather forecast based on which tab is selected.
  ///selectedIndex is 0 by default, when "This Week" is selected, it becomes 1, returning
  ///the daily weather forecast. Used as a child of the final Row in build().
  List<Widget> selectedWeather() {
    if (selectedIndex == 0) {
      return [
        hourlyWeather[0],
        hourlyWeather[1],
        hourlyWeather[2],
        hourlyWeather[3],
        hourlyWeather[4],
        hourlyWeather[5],
        hourlyWeather[6],
        hourlyWeather[7],
        hourlyWeather[8],
        hourlyWeather[9],
        hourlyWeather[10],
        hourlyWeather[11],
      ];
    } else if (selectedIndex == 1) {
      return [
        dailyWeather[0],
        dailyWeather[1],
        dailyWeather[2],
        dailyWeather[3],
        dailyWeather[4],
        dailyWeather[5],
        dailyWeather[6],
      ];
    } else {
      return [OvalWeather("null", 0, "null")];
    }
  }
这些代码所做的只是根据用户的点击返回相应的预测信息。我怀疑这是导致问题的原因,如我的堆栈跟踪所示:
The relevant error-causing widget was:
  HomeScreen file:///E:/Projects/flutter/myapps/clear_skies/lib/screens/LoadingScreen.dart:37:18
When the exception was thrown, this was the stack:
#0      List.[] (dart:core-patch/growable_array.dart:146:60)
#1      _HomeScreenState.**selectedWeather** (package:clear_skies/screens/HomeScreen.dart:356:22)
#2      _HomeScreenState.build (package:clear_skies/screens/HomeScreen.dart:521:41)
#3      StatefulElement.build (package:flutter/src/widgets/framework.dart:4619:28)
#4      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4502:15)
我觉得主要的问题是它在不首先获取信息的情况下加载UI。当然,我不能使build()小部件异步,但是我使方法updateUI()异步,在其中填充selectedWeather()中使用的列表。我无法使selectedWeather()异步,因为listView中Row的 Children 部分需要一个列表,而不是Future。该应用程序会在创建屏幕后仅一秒钟左右的时间内加载并正常运行,但是在 pop 红色屏幕时我无法部署该应用程序。
我试图在initState()中填充天气预报列表,但这会使我的应用程序崩溃。另外,当我仅注释掉那部分代码时,会出现另一个错误,我认为是由相同原因引起的,无需先等待信息即可填充所有内容。

但是,此错误也会在屏幕加载后的瞬间消失。
我该怎么办?

最佳答案

看来问题在于,您正在尝试在列表为空时访问列表hourlyWeatherdailyWeather的元素。当列表为空时,您可以使用其他Container子句仅返回一个空的if(或您想要的其他任何名称)。

List<Widget> selectedWeather() {
  if (hourlyWeather.isEmpty || dailyWeather.isEmpty) {
    return [Container(child: CircularProgressIndicator())];
  } else {
    if (selectedIndex == 0) {
      return [
        hourlyWeather[0],
        hourlyWeather[1],
        hourlyWeather[2],
        hourlyWeather[3],
        hourlyWeather[4],
        hourlyWeather[5],
        hourlyWeather[6],
        hourlyWeather[7],
        hourlyWeather[8],
        hourlyWeather[9],
        hourlyWeather[10],
        hourlyWeather[11],
      ];
    } else if (selectedIndex == 1) {
      return [
        dailyWeather[0],
        dailyWeather[1],
        dailyWeather[2],
        dailyWeather[3],
        dailyWeather[4],
        dailyWeather[5],
        dailyWeather[6],
      ];
    } else {
      return [OvalWeather("null", 0, "null")];
    }
  }
}

10-08 03:27