skip to Main Content

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


  1. using the same TextEditingController for more than TextField 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 a list with specific length of controllers.

    Login or Signup to reply.
  2. Here is a method to create a dynamic list of TextEditingController() for every item/title without having to manually create them:

    List<String> titles = ['name','location'];
    List<TextEditingController> controllers = [];
    
    for (int i = 0; i < titles.length; i++) {
          controllers.add(TextEditingController());
    }
    

    See demo: https://dartpad.dev/?id=5019539e54a2f8434df83e6508d3ceea

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search