{

Dart| Flutter: How to check the type of an 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 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 is 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

Dart| Flutter How to: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example