{

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.

How to Check variable type is a generic or specific type of a list in Dart and Flutter

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
}
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example

Dart| Flutter How to get File name and extension with example