Skip to main content

How to Evaluate a Variable’s Data Type in Dart

Dart is a typed language, which means that every variable has a type. In Dart, data types are classified as either primitive or reference. A variable’s data type is determined by the value it stores. In this blog post, we’ll take a look at how to evaluate a variable’s data type.

You can check what type of data a variable contains by using the runtimeType property or is operator.

var data = ["Good morning", 13354, 7.223344, [], {}, Product('Metal Ring', 300.00)];

//user is and !is
if (data[0] is String) print(data[0]);
if (data[1] is int) print(data[1]);
if (data[2] is! int) print(data[2]);
if (data[3] is List) print('List');
if (data[4] is Object) print('Object');
if (data[5] is Product) print(data[5].toString());

//use runtimeType 
if (data[1].runtimeType == int) print('Int');
if (data[3].runtimeType == [].runtimeType) print('List');

By continuing to use the site, you agree to the use of cookies.