0
I cannot update a Text widget that should show the sum of two Textformfield as long as someone is changing it (Onchanged). What could I be doing wrong? (I’ve put Setstate everywhere and nothing has worked)
Below, one of Textformfield:
child: TextFormField(
                            onChanged: (value) {
                              setState(() {
                                _ComprPeca = value;
                              });
                            },
                            controller: myController,
                            keyboardType: TextInputType.number,
                            decoration: InputDecoration(
                                labelText: 'Comprimento da Peça'),
                          ),
The container that has Text that I want to change:
Container conteiner = Container(
      child: Padding(
          padding: const EdgeInsets.all(20),
          child: Expanded(child: Text(_ComprPeca + _ComprLanca))));
The Controller I will leave:
  final myController = TextEditingController();
  @override
  void initState() {
    super.initState();
    myController.addListener(_printLatestValue);
  }
    @override
  void dispose() {
    // Clean up the controller when the widget is removed from the widget tree.
    // This also removes the _printLatestValue listener.
    myController.dispose();
    super.dispose();
  } 
  _printLatestValue() {
    print("Second text field: ${myController.text}");
    print("comprimento lanca: " + _ComprLanca);
    setState(() {});
  }
						
From what I understand the author of the question wants to know how to sum two typed numbers and display in a Text. In this case I think it is interesting to mention the conversion from String to Double and (maybe) the use of multiple controllers.
– Naslausky
@Naslausky Reading again, really I had misunderstood... But I have already adjusted the answer according to your tip.
– Matheus Ribeiro
Perfect people! My problem was in the fact that when I added up the two values, if one of them was empty the sum did not happen. And I could only realize this after I put a Try Catch into that sum, the debug didn’t take this error and let everything work normally. I did so and managed to solve: Try { _Resultsdouble = (_Comprlanca.isEmpty ? 0.0: double.parse(_Comprlanca)) *1.25 +double.parse(_Comprpeca) / 2;} catch (e) {print(e); } Thank you very much!
– Flávio Ravier