0

I wanted to make like a currency converter so I created this,

final montant = TextEditingController()..text = '500';
double convertEuro(montant) {
  double convtEur = (double.parse(montant.text) / 3.2);
  return convtEur;
}

double converted = 0;

I had a problem with my function here because the function when the screen loads it's null so I created that converted variable ( It's a stupid move I know at least to get rid of the error on my screen but only showing the initial value from my textController above

Text(
    "${double.parse(montant.text)}" +
        "DT = $converted" +
        " €",
 )

Anyway, the function is triggered when a button is pressed.

onPressed: () {
                if (_formKey.currentState.validate()) {
                  converted = convertEuro(montant);
                }
              },

Can anyone help me how to change that variable value and make it change on my screen when the button is pressed?

Screenshot

3
  • 1
    setState(() => converted = convertEuro(montant)); ? Commented Nov 26, 2021 at 18:17
  • @ישואוהבאותך : Thank you . Last question Can I use it on multiple variables on same time .? like updating 3 or 4 variables in same setState? Commented Nov 26, 2021 at 18:36
  • Yes, you can set multiple variables. You can also just call the setState without any code inside of it. something like this: setState(() { }); Commented Nov 26, 2021 at 18:40

1 Answer 1

0

You can do like this:

setState(() {
  converted = convertEuro(montant);
});

In this, you're basically doing a rebuild of the widget. Thus to implement this in your code, put setState under the onPress:

onPressed: () {
  if (_formKey.currentState.validate()) {
    setState(() {
      converted = convertEuro(montant);
    });
  }
},

Sign up to request clarification or add additional context in comments.

2 Comments

Yes great Thank you . Last question Can I use it on multiple variables on same time .? like updating 3 or 4 variables in same setState?
@LearningFlutterDev yes, you can.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.