【问题标题】:RangeError (index): Invalid value: Not in inclusive range 0..13: 14RangeError(索引):无效值:不在包含范围 0..13:14
【发布时间】:2021-03-21 19:36:51
【问题描述】:

我想在 _menuItems 的第 0 个索引处设置自定义值。

这是我到目前为止所做的:

  int _value;
  List<custom.DropdownMenuItem<int>> _menuItems;

  initDropdown() {
    dynamic data = Provider.of<RestApiData>(context, listen: false).data;
    List dataList = data[1]["offers"];

    _menuItems = List<custom.DropdownMenuItem<int>>(dataList.length+1);
    _menuItems[0] = custom.DropdownMenuItem(
      value: -1,
      child: Text(
        'allOffers'.tr().toString(),
        style: GoogleFonts.ubuntu(fontWeight: FontWeight.w500, fontSize: 14.0),
      ),
      decoration: BoxDecoration(
          border:
              Border(bottom: BorderSide(color: Colors.black54, width: 0.5))),
    );

    for (int i = 1; i <= _menuItems.length; i++) {
      _menuItems[i] = custom.DropdownMenuItem(
        value: int.parse(dataList[i]["Offer"]["id"]),
        child: Text(
          "${dataList[i]["Offer"]["name"]}",
          style:
              GoogleFonts.ubuntu(fontWeight: FontWeight.w500, fontSize: 14.0),
        ),
        decoration: BoxDecoration(
            border:
                Border(bottom: BorderSide(color: Colors.black54, width: 0.5))),
      );
    }
  }

  @override
  void initState() {
    super.initState();
    initDropdown();
  }

我已经尝试过这种方式,但得到 RangeError。 任何帮助将不胜感激。 提前致谢。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    RangeError 表示您正在尝试获取索引处不存在的元素。 Y 你的列表有 14 个元素,它的索引从 0 到 13 开始,你试图在索引 14 处获取元素,这是不可能的

    从循环条件中删除相等性,它将起作用

                  
    for (int i = 1; i < _menuItems.length; i++){
               .....................
    }
    

    如果您感到困惑,请在循环中打印i 的值

    【讨论】:

    • 感谢您的回答,但我想在 _menuItems[0] 索引处放置一个自定义值。您建议的解决方案不起作用。
    • 您的意思是,您将在索引 0 处放置一些值,并为其他值提供 for 循环?
    • 是的,没错。其他索引应该由来自api的json数据提供。
    • 我以前试过这种方式,但是自定义值替换了dataList的第一个元素
    • 这不是您想要实现的目标吗?
    【解决方案2】:

    问题解决了。 我忘了从提到的行中减去 1。

    int _value;
      List<custom.DropdownMenuItem<int>> _menuItems;
    
      initDropdown() {
        dynamic data = Provider.of<RestApiData>(context, listen: false).data;
        List dataList = data[1]["offers"];
    
        _menuItems = List<custom.DropdownMenuItem<int>>(dataList.length + 1);
    
        _menuItems[0] = custom.DropdownMenuItem(
          value: -1,
          child: Text(
            'allOffers'.tr().toString(),
            style: GoogleFonts.ubuntu(fontWeight: FontWeight.w500, fontSize: 14.0),
          ),
          decoration: BoxDecoration(
              border:
                  Border(bottom: BorderSide(color: Colors.black54, width: 0.5))),
        );
    
        for (int i = 1; i < _menuItems.length; i++) {
          _menuItems[i] = custom.DropdownMenuItem(
            value: int.parse(dataList[i - 1]["Offer"]["id"]), //**here**
            child: Text(
              "${dataList[i - 1]["Offer"]["name"]}", //**here**
              style:
                  GoogleFonts.ubuntu(fontWeight: FontWeight.w500, fontSize: 14.0),
            ),
            decoration: BoxDecoration(
                border:
                    Border(bottom: BorderSide(color: Colors.black54, width: 0.5))),
          );
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-07
      • 2021-07-08
      • 2021-02-04
      • 2021-08-14
      • 2021-06-16
      • 1970-01-01
      • 2019-11-01
      • 2020-07-06
      相关资源
      最近更新 更多