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');"
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");
CodePudding user response:
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
}
CodePudding user response:
Try this:
twoMark ??= '0';
final intTwooMark = int.parse(twoMark);
You'll obtain a more readable dart-ish code with just two simple lines of code.