Home > Net >  How to write If statement in dart?
How to write If statement in dart?

Time:11-09

I am writing if statement like that:

String value;
if(int.parse(value)) // I want to write a condition that if error appear while parsing 
// (like value contains some strings) then if statement run

It can be achieved by writing try/catch

try{ 
int.parse(value)
}
catch (e) {
// implement if statement
}

But I want to do this with if statement

CodePudding user response:

Well you can try a different approach using tryParse() method and check whether the result is null then the parse failed.

if (int.tryParse(value)==null){ // execute failed parse code }
  • Related