I have a form with about 15 textform fields to input data by the user. What is the best solution for this?
I have currently created a list like below:
List<TextEditingController> textController = [
TextEditingController(), //name
TextEditingController(), //location
TextEditingController(), //days
TextEditingController(), //year
TextEditingController(), //hour
TextEditingController(), //minutes
TextEditingController(), //seconds
TextEditingController(), //lat_dec
TextEditingController(), //lng_dec
TextEditingController(), //lat_deg
TextEditingController(), //lat_min
TextEditingController(), //lat_sec
TextEditingController(), //lng_deg
TextEditingController(), //lng_min
TextEditingController(), //lat_sec
];
Can I reuse the the same TextEditingController to avoid so many variables?
Any suggestions?
2
Answers
using the same
TextEditingController
for more thanTextField
has unexpected behavior which is:The text you are typing in a specific field will be simultaneously written to other fields that are controlled with the same controller.
so, try to handle it through creating a
map of controllers
or even generate alist
with specific length of controllers.Here is a method to create a dynamic list of
TextEditingController()
for every item/title without having to manually create them:See demo: https://dartpad.dev/?id=5019539e54a2f8434df83e6508d3ceea