Flutter:Array 的 Textformfield 验证器不工作

Flutter:Array of Textformfield validator not working

我正在处理一组文本表单域。 Array 可以超过 50 个。我想将数组值提交给服务器。问题是当我执行验证器时,它将在没有错误消息之后验证数组中的前 8 个。滚动时也会消失。我在下面发布了我的代码:

void validateAndSave() {
    final form = _formKey.currentState;
    if (form.validate()) {

      _formKey.currentState.save();
      print('Form is valid');
    }
    else {
    //  _formKey.currentState.
      print('form is invalid');
    }

  }

    Widget singleItemList(int index) {
    Item item = itemList[index];

    return Container(
      width: MediaQuery.of(context).size.width,
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      child:
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [






          Expanded(

            child:Padding(
              padding: const EdgeInsets.fromLTRB(20, 10, 10, 20),


              child: new TextFormField(
               controller: _controller[index],

              keyboardType: TextInputType.number,




                //onSubmitted: (text) {
              //  takeNumber(text, item.id);
              //},


                validator: (value) {
                  if (value.isEmpty) return 'value is required';

                },



                decoration: new InputDecoration(

                  border: new OutlineInputBorder(
                      borderSide: new BorderSide(color: Colors.indigo)),




                  labelText: itemList[index].id,

                  suffixStyle: const TextStyle(color: Colors.red)
              ),


              ),
            ),
          ),


        ],
      ),


    );

  }

请帮帮我。提前致谢。

此致, 萨西什

您是否考虑过用 Column 替换 ListView.builder?
像这样:

child: SingleChildScrollView(
    child: Column(
      children: List.generate(itemList.length, (index) {
        if (itemList.isEmpty) {
          return CircularProgressIndicator();
        } else {
          return singleItemList(index);
        }
      })
    ) 
  )

来自 ListView documentation:

Destruction
When a child is scrolled out of view, the associated element subtree, states and render objects are destroyed. A new child at the same position in the list will be lazily recreated along with new elements, states and render objects when it is scrolled back.