无效值:有效值范围为空:0

时间:2020-04-04 06:48:51

标签: arrays flutter dart

我有两个数组,我想为第一个数组(_filteredSaleRef)的每条记录创建带有第二个数组(filteredShopItem)的下拉列表

这给了我无效值:有效值范围为空:0 此错误,但是当我删除DropdownButton时,它工作正常。

这是我到目前为止所做的事情

Widget build(BuildContext context) {

    List<SaleRef> _selectedRef = List.generate(filteredShopItem.length, (index) => SaleRef());

    return new Scaffold(
        body: Column(
      children: <Widget>[

        Expanded(
          child: PageView(
            controller: PageController(viewportFraction: 1),
            scrollDirection: Axis.horizontal,
            pageSnapping: true,
            children: <Widget>[
              ListView.separated(
                itemBuilder: (context, index) {
                  return Row(
                    children: <Widget>[
                      Container(
                        alignment: Alignment.bottomLeft,
                        height: 40,
                        width: MediaQuery.of(context).size.width * 2 / 5,                        
                        child: Padding(
                          padding:
                              const EdgeInsets.only(left: 30.0, bottom: 10),
                          child: Text(filteredShopItem[index].name +
                              " , " +
                              filteredShopItem[index].city),
                        ),
                      ),                      
                      Container(
                        height: 40,
                        width: MediaQuery.of(context).size.width * 1 / 5,                        
                        child: DropdownButton<SaleRef>(
                          hint: Text("  Select Ref"),
                          value: _selectedRef[index],
                          onChanged: (SaleRef value) {
                            setState(() {
                              _selectedRef[index] = value;
                              print(_selectedRef[index].refID);
                            });
                          },
                          items: _filteredSaleRef.map((SaleRef ref) {
                            return DropdownMenuItem<SaleRef>(
                              value: ref,
                              child: Row(
                                children: <Widget>[
                                  Text(
                                    "  " + ref.refName,
                                    style: TextStyle(color: Colors.black),
                                  ),
                                ],
                              ),
                            );
                          }).toList(),
                        ),
                      ),

                    ],
                  );
                },
                separatorBuilder: (context, index) {
                  return Divider(height: 16);
                },
                itemCount: filteredShopItem.length,
              )
            ],
          ),
        ),
      ],
    ));
  }

1 个答案:

答案 0 :(得分:0)

在课堂外创建一个 size 的数组可以帮助我解决问题

List<SaleRef> _selectedRef = new List(filteredShopItem.length);

然后我像这样使用它

                         DropdownButton<SaleRef>(
                          hint: Text("  Select Ref"),
                          value: _selectedRef[index],
                          onChanged: (SaleRef value) {
                            setState(() {
                              _selectedRef[index] = value;
                              print(_selectedRef[index].refID);
                            });
                          }