【问题标题】:ItemList: RangeError (index): Invalid value: Valid value range is empty: 0ItemList:RangeError(索引):无效值:有效值范围为空:0
【发布时间】:2021-07-28 22:57:28
【问题描述】:

我知道很多主题都是用这个标题创建的,但没有针对这种特定类型的答案。

我收到以下错误代码: 错误:RangeError(索引):无效值:有效值范围为空:0

当 REST-API 中没有任何数据时,输出为:[]

我不知道为什么会收到此错误消息,请帮助我解决它...

body: blockitem.listitems == null
          ? Center(child: CircularProgressIndicator())
          : ItemList(),
ListView.builder(
      itemCount: blockitem.listitems[tab.currentTab] == null
          ? 0
          : selectedList.length,
      itemBuilder: (BuildContext context, int index) {
        return Card(
          elevation: 2.0,
          color: Colors.blue.shade50,
          child: ListTile(
            leading: getValidityIcon(
                selectedList[index].isValid,
                selectedList[index].targetAge),
            title: Text(selectedList[index].name),
            subtitle: Text(
                "Expires at: ${selectedList[index].expDate}"),
            trailing: Icon(
              Icons.arrow_forward_ios,
              color: Colors.green.shade400,
            ),
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => ItemDetail(itemDetailed: selectedList[index]),
                ),
              );
            },
          ),
        );

【问题讨论】:

    标签: json flutter dart


    【解决方案1】:
    itemCount: blockitem.listitems[tab.currentTab] == null
    

    输出不为空。它只是空的。你可以检查它的大小。所以像这样改变你的代码;

    blockitem.listitems.length == 0 ? .....
    

    【讨论】:

      猜你喜欢
      • 2019-07-25
      • 2020-12-02
      • 2021-10-06
      • 2021-11-13
      • 2021-06-16
      • 2020-06-06
      • 1970-01-01
      • 2020-10-07
      • 2021-12-31
      相关资源
      最近更新 更多