0

I am iterating over list and adding the _controller and then I am trying to access the controller inside using indexing : _controllers[index][titleValue] And, it is working, the first controller is getting assign to to for the first element but, then for the rest other element it is not working....

can anyone pls suggestion where I might be going wrong? or

what could be better solution to implement TextEditingController for each element inside the ListView.builder()?

List<Map<String, TextEditingController>> _controllers = [];

void extractItem(List<dynamic> itemDict) {
    for (final item in widget.itemDict) {
      _controllers.add({
        item['Item_name'] as String: TextEditingController(),
      });
    }
  }```
Harshit
  • 95
  • 2
  • 8
  • 1
    Can you include how and where you are using these widgets. A sample widget will be helpful that will reproduce the same errors. Do you need items_name as mandatory, you can also use `index` here – Md. Yeasin Sheikh Nov 22 '21 at 06:02
  • @YeasinSheikh I am using this widget to get user input for each element that is in the list... this is the code of the widget... [link](https://pastebin.com/raw/w9yEFSKJ) ........... – Harshit Nov 22 '21 at 07:35
  • @YeasinSheikh When I am using the index then for every element same controller is getting assign.... Below, I also add the screen short... 1st screen-short you can see 3 item [link](https://cdn.discordapp.com/attachments/797453214563041293/912246088533151754/unknown.png) in the screen-short 2 I want user to input info about each item... link[https://cdn.discordapp.com/attachments/797453214563041293/912246567992451072/ss1.jpg] – Harshit Nov 22 '21 at 07:43
  • you need `final _controllers = {};` and `_controllers[item['Item_name']] = TextEditingController();` inside the loop – pskink Nov 22 '21 at 07:50
  • sure, your welcome, btw you dont have to create all the controllers at once: instead do it on demand inside `itemBuilder` callback – pskink Nov 22 '21 at 08:21
  • @pskink I tried doing it but, for every element same controller is getting assign.... so, and also some people suggested to not create controller in build widget that later might cause some issue... so, due to that now I am creating it initState() – Harshit Nov 22 '21 at 08:40
  • 1
    use `_controllers.putIfAbsent` inside `itemBuilder` – pskink Nov 22 '21 at 08:54

0 Answers0