Muhammad Amir
Muhammad Amir

Reputation: 672

Check if variable type is List<dynamic>, flutter

Below is the code where I am first printing the type of myVar. It returns List<dynamic> in the console. But when I compare its type in a if condition, it does not pass this condition.

print(myVar.runtimeType);
if (myVar.runtimeType is List<dynamic>) { 
    print("Yes");
}

What's wrong here?

Upvotes: 15

Views: 18326

Answers (1)

Ali Qanbari
Ali Qanbari

Reputation: 3111

runtimeType is of type Type and never going to be List or int.

The is operator automatically compares types.

Also when writing a type, if you leave the generic parameter empty, it will be read as a dynamic. for example List and List<dynamic> have the same type.

if (myVar is List) { 
    print("Yes");
}

Upvotes: 41

Related Questions