skip to Main Content

First index value is 10. Second index value is 5

I am expecting the next index value to be 10 * 5 = 50, but I am getting the same value for all the index. Why does this happen?

expecting output

void takeCostingValue(String text, String index) {
    Map < String, int > quantities = {};
    try {
        int number = int.parse(text);
        quantities[index] = number;

        var totalObj = quantities.entries.map((e) {
            return e.value;
        }).toList();

        var result = totalObj[0] * totalObj[1];

        setState(() {
            _costtextController = result as TextEditingController;
        });
    }
}

ListView.builder(
    itemCount: costNameListData.length,
    itemBuilder: (context, index) {
        return TextField(
            controller: _costtextController,
            onChanged: (text) {
                takeCostingValue(
                    text, index);
            },
        ),
    },
),

2

Answers


  1. Chosen as BEST ANSWER
    List<TextEditingController> textEditingControllers = [];
     @override
      void initState() {
        super.initState();
        JsonListData.forEach((dynamic str) {
                var textEditingController = TextEditingController(text: '');
                textEditingControllers.add(textEditingController);
              });
      }
    
      Widget singleItemList(int index) {
        return Container(
          child: Row(
            children: [
              Expanded(
                flex: 3,
    child: Row(
        children: [
    Container(
                      alignment: Alignment.centerLeft,
                      width: 68,
                      height: 50,
                      child: Text(
                        '${JsonListData[index]['CostName'] ?? ''} :',
                      ),
              ),
    ]),
    ),
    TextField(controller:textEditingControllers[index],
              keyboardType:TextInputType.numberWithOptions(decimal: true, signed: false),
               inputFormatters: [FilteringTextInputFormatter.allow(RegExp('^$|^(0|([1-9][0-9]{0,}))(\.[0-9]{0,})?$')),
                 ],
    
    onChanged: (text) {                                
    takeCostingValue(
     text, '${[index]}');
    

    },), ], ), ); }

    void takeCostingValue(String text, String i) {
    try {
    if (i == '0') {
    double value1 = double.parse(value);
            final value_2 = textEditingControllers[1].text == ''
                ? 1.0
                : textEditingControllers[1].text;
            double value2 = double.parse('${value_2}');
            final res = ((value1) * (value2));
    
            setState(() {
              textEditingControllers[2] =
                  TextEditingController(text: '${res}');
            });
    }else if (i == '1') {
    
     final value_1 = textEditingControllers[0].text == ''
                ? 1.0
                : textEditingControllers[0].text;
            double value2 = double.parse(value);
            double value1 = double.parse('${value_1}');
                    final res = ((value1) * (value2));
            setState(() {
              textEditingControllers[3] =
                  TextEditingController(text: '${res}');
            });
    
    }
    }on FormatException {}
    }
    

  2. you should provide different TextEditingController for every TextField in yours
    ListView:_fabricCostTextController, _averageCostTextController and _costtextController. this should be the different instance of TextEditingController.
    in other words, you can not use _costtextController for every TextField.

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