Ad

How To Write If Statement In Dart?

- 1 answer

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

Ad

Answer

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 }
Ad
source: stackoverflow.com
Ad