Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IF/ELSE Statement doesn't work with int.parse?

I really don’t get it. "twoMark" and "twoTente" are null so I made an If/else statement to convert them into 0 if they are indeed null or if not, just int.parse the value… but the code just IGNORE the IF and go straight to the else even tho the value is null so it is supposed to stop after it.

SO i get this error: Unhandled Exception: FormatException: Invalid radix-10 number (at character 1).

Because for my understanding he is trying to do the "twoMark = int.parse(twoMark ??= ‘0’);" instead of stopping at "twoMark = int.parse(twoMark == null ? twoMark : ‘0’);"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

if(twoMark == null){
    print("FGM EST NULLE");
    twoMark = int.parse(twoMark == null ? twoMark : '0');
  }else{
    print(" FGM N'EST PAS NULLE");
    twoMark = int.parse(twoMark ??= '0');

  }

my console is printing the second print even tho "twoMark" IS NULL.

———– FULL CODE ———

avePicture(pathing, idTeamPlayer, fPlayerName, lPlayerName, phonePlayer, age,
    taille, jerNumb, position, fgm, fga, context) async {
  SharedPreferences sp = await _pref;
  final statAccess = DatabaseModel();
  int? idPlayer = sp.getInt("idPlayer");
  idTeamPlayer = sp.getInt("idTeam");
  phonePlayer = phonePlayer.replaceAll(RegExp('[^0-9]'), '');
  fPlayerName = fPlayerName.replaceAll(RegExp('[^A-Za-z]'), '');
  lPlayerName = lPlayerName.replaceAll(RegExp('[^A-Za-z]'), '');
  var twoTente = fga;
  var twoMark = fgm;
  if(jerNumb != null){
    jerNumb = jerNumb.replaceAll(RegExp('[^0-9]'), '');
  }
  twoMark ??= '0';
  final intTwooMark = int.parse(twoMark);
  print("THE TWO $intTwooMark");

>Solution :

int.parse expect a string inside it, Use int.tryParse to avoid getting exception.

use

 final intTwooMark = int.tryParse(twoMark.toString())??0;

You can do

twoMark = int.tryParse("${twoMark == null ? twoMark : '0'}")??0; // not necessarily have to be like this, just an example

Or

twoMark = int.tryParse(twoMark.toString())??0;

Or just, it will assign right part if the left is null

twoMark??=0;
if (twoMark == null) {
  print("FGM EST NULLE");
  twoMark ??= 0; //or just  twoMark = 0;
} else {
  print(" FGM N'EST PAS NULLE");
  twoMark = twoMark; // no need
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading