Flutter从Form中提取所有键值 [英] Flutter extract all key-values from Form

查看:554
本文介绍了Flutter从Form中提取所有键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter构建具有动态输入集的表单。
因此,我永远无法假设表单将包含多少个文本字段,因此,我无法手动为每个字段分配一个键以以后从其控制器中检索数据。

I am building a form using flutter, that has a dynamic set of input. Therefore, I can never assume how many text fields the form will have, and so I cannot manually assign a key to each field to later retrieve data from its controller.

如果我实例化一个 Form ,其中包含一些 TextFormField 如何简单地提取整个表单的键值数组,

If I instanciate a Form, that holds some TextFormField how can I simply extract an array of key values for the whole form, when pressing a button for example?

Form(
  key: _formKey,
  child: Column(
    children: [
       TextFormField(),
       TextFormField(),
       TextFormField(),
    ]
  )
)


推荐答案

即时生成TextField及其控制器

to generate TextFields and their controller on the fly

Map<int,TextEditingController> controllers = {};



        Form(
            key: _formKey,
            child: Column(children: [
              ...List.generate(length, (index) {
                var controller = controllers.putIfAbsent(
                    index, () => TextEditingController());
                return TextFormField(
                  controller: controller,
                );
              })
            ]));

您可以使用其索引访问给定地图中的所有控制器

you can access all the controllers in the given map with their index

这篇关于Flutter从Form中提取所有键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆