Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter evaluate if var is integer or string

Tags:

flutter

i need to evaluate what type is a variable to make some switch,there are any way to evaluate a varible to get his type, like val() or something similar. i need to do something for integers and other for string.

i alreaedy try to using a switch, like this,

 switch (selector) { case  int :   print('value is a integer');     break; case  String:     print('value is a String');    break; 

}

but how i do this, if switch can allow compare mixed type of vars?

thank you

like image 897
ALEXANDER LOZANO Avatar asked Jul 11 '18 11:07

ALEXANDER LOZANO


People also ask

How do you determine the type of variable in flutter?

To check the type of a variable in Flutter and Dart, you can use the runtimeType property.

How do you check if a value is an integer in flutter?

It is easy to check if a number is an int , just do value is int . The slightly harder task is to check whether a double value has an integer value, or no fractional part. There is no simple function answering that, but you can do value == value. roundToDouble() .

How do you check if a string is an integer in flutter?

To check whether a string is a numeric string, you can use the double. tryParse() method. If the return equals null then the input is not a numeric string, otherwise, it is. Hope this helps.

How do you check Dart data type?

Dart objects have runtimeType property which returns Type . To check whether the object has a certain type, use == operator. Unlike is , it will only return true if compared to an exectly same type, which means comparing it with its super class will return false .

How to call a variable inside string in flutter?

Calling a variable inside string is extremely easy in Flutter. You just need to use the dollar syntax as given below. Here $value represents an already defined variable named value. Still unclear? See the complete Flutter example given below.

How to find the type of a variable in Dart/flutter?

Another way to find the type of a variable in Dart/Flutter is by using is operator which is similar to instanceof operator in JavaScript. Here again pay attention to the int inside the ‘if’ condition which is not a string.

What does $value mean in flutter?

Here $value represents an already defined variable named value. Still unclear? See the complete Flutter example given below.

Can a string variable be an integer forever?

Importantly, this variable must be a String forever. You cannot re-assign the variable as an integer. If you did want to create a variable that's more dynamic, you'd use the dynamic keyword. We'll see examples of that in a later lesson.


1 Answers

You can use the keyword is or switch over runtimeType :

dynamic foo = 42; if (foo is int) {   print("Hello"); } switch (foo.runtimeType) {   case int: {     print("World");   } } 

Consider using is instead of directly using runtimeType. As is works with subclasses. While using runtimeType is a strict comparison.

like image 165
Rémi Rousselet Avatar answered Oct 02 '22 06:10

Rémi Rousselet