8

What is the best way to set an array of TextEditingController in a flutter . I mean I need to get a value array of Textfield(1 to n) value and sent to the server.

Can anyone help how to achieve this?

I tried

for(int i=1;i<75;i++) { 
TextEditingController _controller[i] = TextEditingController(); 
}

Regards, Sathish

sathish kumar
  • 1,061
  • 6
  • 16
  • 31
  • dont get your question. A TextEditingController can only handle one FormField at a time as far as i know. But if you could provide code of what you have done so far... this would help... – Logemann Oct 17 '19 at 13:18
  • for(int i=1;i<75;i++) { TextEditingController _controller[i] = TextEditingController(); } – sathish kumar Oct 17 '19 at 13:19
  • If you just want for getting value then I suggest creating array of value and store value on onChanged of Textfield – Dhaval Solanki Oct 17 '19 at 13:22
  • i want to validate all textfield and how to achieve it. validator is not in TextField thats the issue i am facing. – sathish kumar Oct 17 '19 at 13:25
  • Did you mean validate when typing `_controller[i].addListener((){ // validate })`? or `TextFormField` with `valdiate` callback? – Tokenyet Oct 17 '19 at 13:48

3 Answers3

28

There are plenty of ways to do that

List<TextEditingController> _controller = List.generate(74, (i) => TextEditingController());

or

List<TextEditingController> _controller = [];
for (int i = 1; i < 75; i++) _controller.add(TextEditingController());

or

List<TextEditingController> _controller = [
  for (int i = 1; i < 75; i++)
    TextEditingController()
];
Igor Kharakhordin
  • 9,185
  • 3
  • 40
  • 39
0

You have to add a list of TextEditingController and need to add the contoller text to that list and parse it as you need.

  List<String> selection = [];  
 List<Product> productList = []; 

 //---------Adding contoller to list   
  
 productProvider.getAll(user.guid).forEach((element) {//---List<Product>
 final TextEditingController quantityController = 
 TextEditingController(text: element.quantity);
 quantityControllers.add(quantityController);
 });

   //-------Adding list of products to list
  List<Map<String, dynamic>> productItems = [];
   List<Product> productOriginalList = 
   productProvider.getAll(user.guid);
   for (int i = 0; i < productOriginalList.length; i++) {
   final Product product = productOriginalList[i];
   if (selection.contains(product.equipmentId)) {
               
   productItems.add(product.toJson(quantityControllers[i].text));
              }

 /* Map<String, dynamic> toJson(String quan) => {
'ProductId': id,
'Quantity': quan,
 };     
TextField(
controller: quantityControllers[index],*/
Tanvir Ahmed
  • 564
  • 6
  • 13
0

If you are using a dynamic widgets you can use -

TextEditingController _controller = new  TextEditingController(); 

inside your dynamic widget.This will automatically create new textediting controller evertime your widget runs. and at last you can store that value in your list.

Example :-

List <String>controllers = [];
controllers.add(_controller.text.toString)