I have to check runtime Type of value for this I am using :-

for Example:-

String a = "abc";

int b = 123;

var c = "123"; //this is int value but because of this double quotes is represent as a String

a.runtimeType == String //true

b.runtimeType == int // true

c.runtimeType == String //true

c.runtimeType == int //false

a = "abc" // okay

b = 123 //okay

c = "123" //is issue

now I have to call a api with only String body in this case :-

this c is called the API because is String but i know this is a int value which is present as a String, so I have to stop this.

How can I check this??

when I am using try catch so my program is stopped because of FormatException error.

Note:- I don't know the real value of C, may be its "123" or "65dev" or "strjf" this value is changed every time.

and if i am parsing this in int to this return an error in many case.

3

There are 3 best solutions below

6
Muhammad Waqas On

I don't know how you're passing it to API but if you wanna pass integer value from string quoted variable, you can parse/convert to integer like this.

int.parse(c);

either you can pass it directly or you can store in another variable and pass that variable. Alternatively if you've int value and to have to pass it as a string, simply parse like this

integerValue.toString(); according to your code

b.toString();

Edit

  String a = '20';
  String b = 'a20';
  
  try{
  int check = int.parse(a);
 //call your api inside try then inside if
  if(check.runtimeType == int){
    print('parsed $check');
     }
  }
  catch(e){
    
    print('not parsed ');
    //handle your error
    throw(e);
  }
4
Syed Ibrahim On

Ok i understood that you want to pass "123" by checking and if it is int you are passing it , My question is what you will do if it is "123fe" you are going to pass as string? or you will pass nothing.

0
Syed Ibrahim On

This will definitely help you!

String name = "5Syed8Ibrahim";
final RegExp nameRegExp = RegExp(r'^[a-zA-Z ][a-zA-Z ]*[a-zA-Z ]$');
print(nameRegExp.hasMatch(name));


//output false


name = "syed ibrahim";
print(nameRegExp.hasMatch(name));


//output true

Just check the output and based on that boolean value invoke api call

I hope it will done the work