0

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");

1
  • int.parse(twoMark == null ? twoMark : '0') is backwards. If twoMark is null, you're calling int.parse(null). The twoMark == null check is redundant anyway since you already checked it in the outer if. Commented Aug 16, 2022 at 20:35

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

It is better to use int.tryParse to avoid numeric string exception
Nothing works, I have even tried to do "if (twoTente == null){print("hello"}; but it doesn't even print it !!
Can you include full snippet
Try final intTwooMark = int.tryParse(twoMark.toString())??0; and it is better to avoid using var to have more control over code writing
Then I suspect the issue was the string wasn't a numeric value. Always try using .tryParse
|
1

Try this:

twoMark ??= '0';
final intTwoMark = int.parse(twoMark);

You'll obtain a more readable dart-ish code with just two simple lines of code.

EDIT. If you aren't sure your string is actually convertible, use tryParse and / or use a try catch block and decide whatever you want to do with it, e.g. assign null to intToMark

3 Comments

It doesn't work : Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)
@zsupraz Then twoMark is a non-null String that does not represent a number. Either: A. Fix the input. B. catch the FormatException. C. Use int.tryParse and handle it returning null.
yeah the "tryParse" worked for me, I didn't know it exist I am new to flutter. Thx for the help !

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.