Search code examples
flutterdartlistview

Set flutter textfield inside Listview and get the value of each textfield on submit


controller:

List<TextEditingController> areaController = [];

Code:

Column(
      children: [
        ListView.builder(
          shrinkWrap: true,
          itemCount: 5,
          itemBuilder: (context, index) {
            areaController.add(TextEditingController());
            return TextField(
              controller: areaController[index],
              keyboardType: TextInputType.number,
              onChanged: (text) {},
              decoration: const InputDecoration(
                labelText: "Area",
              ),
            );
          },
        ),
        const SizedBox(
          height: 30,
        ),
        ElevatedButton(
          child: const Text('Submit'),
          onPressed: () {
            //get the value of every index textfield
          },
        ),
      ],
    ),

How can I set flutter textfield inside Listview and get the value of each textfield on submit


Solution

  • Why don't you use a for-loop? just call .text on each TextField to get the text value.

    ElevatedButton(
                  child: const Text('Submit'),
                  onPressed: () {
                    for (int i = 0; i < areaController.length; i++) {
                      print(areaController[i].text);
                    }
                  },
                ),