Home > Back-end >  A function error preventing screen load in Flutter
A function error preventing screen load in Flutter

Time:11-21

so i got 2 screens in my flutter app. It's an app to simulate an online pay. Anyway , I have this function on my second screen :

final salaire = TextEditingController();
final valeurAcq = TextEditingController();
final valeurApport = TextEditingController();
final moisRembours = TextEditingController();
final typeCreditChoisis = TextEditingController();

  double calcul(salaire, valeurAcq, valeurApport, moisRembours)
  {
  double res = (double.parse(salaire.text) -
  double.parse(valeurApport.text) / double.parse(moisRembours.text));
  return res;
  }

This function is triggered with onPressed in a button in second screen after filling a form.

child: MaterialButton(
minWidth: double.infinity,
height: 60,
onPressed: () {
calculCredit(salaire, valeurAcq,
valeurApport, moisRembours);
},

So , I launched my simulator and on my first screen when I click on a button that should bring the second page , I got an error in my function .

Assuming the problem is that Dart failed to parse the controller to double and causing an exception.

PS: The second screen won't open.Error

Error :

FormatException (FormatException: Invalid double)

CodePudding user response:

Test With this :

double res = (double.parse(salaire.text ?? "0") -
      (double.parse(valeurApport.text??  "0") / double.parse(moisRembours.text ?? "0" ))

CodePudding user response:

Adding initial value to my inputs using my controllers fixed the problem.

final salaire = TextEditingController()..text = '0';
final valeurAcq = TextEditingController()..text = '0';
final valeurApport = TextEditingController()..text = '0';
final moisRembours = TextEditingController()..text = '0';
  • Related