Dart Programming - Data Types

Published May 07, 2020


One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language

Dart has below Data types

Numbers

dart has two types of int and double, both of which are subclasses of the num class

The int type depends on the platform, and the integer value is not greater than 64 bits. Dart on the VM, the value from -2 63 is to 2 63 is -1 , the compiled JavaScript Dart JavaScript code, allowing values from -2 53 is to 2 53 is -. 1

The double type is a 64-bit double-precision floating-point number specified by the IEEE 754 standard

void main() {
  var intValue = 100;
  print(intValue.runtimeType); //int

  var doubleValue = 100.0;
  print(doubleValue.runtimeType); //double

  num numValue = 100;
  print(numValue.runtimeType); //int
  numValue = 100.0;
  print(numValue.runtimeType); //double
}

 

Some common digital type conversion methods

print(num.parse("2000"));
  print(int.parse("200"));
  print(double.parse("121"));

 

Strings

In addition to declaring a string through single or double quotes, you can also declare a combined string through adjacent string literals (equivalent to using + to add the strings as a whole)

var stringValue = "Double";

  var stringValue2 = 'Single';

  var stringValue3 = "Both "
      "Double"
      'Single';

  print(stringValue3);

 

Output

Both DoubleSingle

 

Boolean

The dart language also uses the bool keyword to indicate the true or false of things. Only two objects have bool types: true and false, both of which are compile-time constants. And dart is a strong bool type check, only the value of bool type is true is considered true

List

list is also one of the most common data types, dart declares list variables through square brackets

Because dart has type deduction, listValue is automatically assigned to List <int> type, so after declaring listValue, you cannot add values ??of other types of variables

var listValue = [1, 2, 3];
  // listValue.add("4"); error
  print(listValue.runtimeType);

 

If we want to add variables of different data types to List, we need to directly indicate that the data type is Object

var listValue = <Object>[1, 2, 3];
  listValue.add("4");
  print(listValue.runtimeType);

 

In most cases, in order to limit the storable data type of List, directly indicate the data type when using

var intList = <int>[1, 2, 3, 4];

If the constructor that specifies the collection size is called when the List is declared, the collection size is fixed and the length of the list cannot be changed at runtime

 var list = List<int>(2);
  list.add(2); //error  Unsupported operation: Cannot add to a fixed-length list

 

Set

Set is a data collection that does not contain duplicate data, and its usage is basically similar to List

void main() {
  var list = [1, 2, 2, 3, 4, 5, 5];
  var set = Set.from(list);
  print(set); //{1, 2, 3, 4, 5}
}

 

Map
map is a data type that associates keys and values, and keys and values ??can be any type of object

void main() {
  var mapValue = {"name": "github", "age": 24};
  mapValue["url"] = "https://github.com/";
  print(mapValue); //{name: github, age: 24, url: https://github.com/}
  print(mapValue.length); //3
  print(mapValue.runtimeType); //_InternalLinkedHashMap<String, Object>
}

 

Dynamic Type

Dart is an optionally typed language. If the type of a variable is not explicitly specified, the variable’s type is dynamic. The dynamic keyword can also be used as a type annotation explicitly

 


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

25 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions