{

Dart| Flutter: How to check the type of a variable is list| Flutter By Example


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

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
}
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

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

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

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart