Dart| Flutter: How to check the type of a variable is list| Flutter By Example
This is a simple post to check variable is of a specific or Generic List type.
Dart provides an is
operator that checks the type of a variable at runtime and returns true for a given variable with a predefined type or not.
How to check if a variable is a List in Dart
This is
operator provides a variable and List
type and returns true if a given variable of type List.
Here is an example.
void main() {
List<String> words = ["one", "two", "three"];
print(words is List); //true
if (words is List) {
print(words); //[one, two, three]
}
}
Output:
true
[one, two, three]
The above checks the generic List type.
How do you check a data type of a list of values?
Check the type of data that is stored in Dart programming
The above is used, to check a type of variable, that only checks a generic List.
Dart provides a Generic List to store String, int, double and dynamic values.
dynamic values are types that accept any dynamic type.
Here is an example to check the type of variable String or int or double or dynamic list of data.
void main() {
List<String> words = ["one", "two", "three"];
print(words is List); //true
print(words is List<String>); //true
print(words is List<int>); //false
List<int> numbers = [1, 5, 6];
print(numbers is List); //true
print(numbers is List<String>); //true
print(numbers is List<double>); //false
List<double> doubles = [1.1, 5.3, 6.2];
print(doubles is List); //true
print(numbers is List<int>); //true
print(doubles is List<String>); //false
List<dynamic> dynamicsvalues = [1.1, "5.3", 6.2];
print(dynamicsvalues is List); //true
print(dynamicsvalues is List<dynamic>); //true
print(dynamicsvalues is List<int>); //false
}