Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


Understanding Data Types in Dart Programming

dart data types


Data type is a categorization that determines which type of value a variable will hold. Once the value that a certain variable holds is determined, operations such as mathematical, logical, or relational are utilized on the said variable.

In clear terms, data types define the kinds of operations that can be performed on variables.

The Dart-specific data types and the corresponding keywords can be utilized along with those data types.

dart keywords


1. Number

The Number data type in Dart is utilized by a variable to hold a numerical value. If a variable is classified as a number, then it holds nothing but a numerical value. The number is categorized into two types.

Following are the DART keywords for the Number data type:

  • Int represents whole numbers. It is specified and declared using the int keyword. An integer is allocated four bytes in memory and can have both non-decimal positive and negative values.
    Example:
    Int price=450;

  • The double data type represents 64-bit floating-point numbers. Eight bytes are allocated to each double variable. If a double variable is assigned an integer value, the integer value will be typecast and made into Ä‘ouble.
    Example:
    Double num = 5.5;

2. String

The String data type represents the concatenation of several characters. A value for the variable declared as a String will be encased either in single or double quotes. Declaration of a variable belonging to this type can be done using the string keyword. It is important to note that the String data type follows the 16-bit Unicode Transformation Format sequence (UTF-16). UTF-16 is an encoding of Unicode in which each character is composed of either one or two 16-bit elements.

3. Boolean

The Boolean data type can hold a value of 'true' or 'false'. Variables specified with Boolean are declared with the bool keyword. They are mostly used in decision-making statements.

Example:

Bool value = true;

4. List

The List data type represents a list of entities or things using a single variable. This list can only hold an ordered group of items. Another fact to keep in mind is that List will have values separated from one another using commas and is enclosed within square braces or brackets [].

Example:

Var mark = new list (2);

Marks [0] = 72;

Marks [1] = 63;

Following are the classifications of the list:

  • Fixed length list - This kind of list cannot be changed or modified at runtime.
    Var marks = List. Filled (2,0);
    Marks [0] = 72;
    Marks [1) = 63;

  • Gowable List - This specific list can be altered or modified at runtime.
    Var marks = [10, 20];
    Marks. Add (72);
    Marks. Add (63);

5. Map

The Map data type is utilized to represent data in key and value form. The items stored in this particular type can only be accessed using the associated key provided for the Map object.

A Map variable, which is inside curly braces has each key-value pair separated from one another using commas. In Dart, a map can be declared using Map constructors or Map literals.

The following are the most commonly used map types:

  • HashMap
  • LinkedHashMap
  • SplayTreeMap

6. Dynamic

The dynamic data type, as pointed out by the name, is dynamic and chooses not to be initialized with a value at declaration time. A variable declared dynamic can store data of any type.

The value assigned to a variable of type dynamic can be modified at any given point. A variable categorized as dynamic is declared using the dynamic keyword.

7. The constants

Dart Constants are immutable objects whose values can neither be changed nor modified during program execution. It is impossible to reassign Constant after initial initialization. It must be initialized when declared.

The following are keywords to be utilized for creating constants in Dart:

  • final
  • const

The final keyword is used to hardcode the values of the variable and it cannot be altered in the future.

The const keyword in Dart behaves exactly like the £inal keyword. The only difference between final and const is that const makes the variable constant at compile-time only.

8. Arrays

An array is one of the most commonly used data types used to represent data collection in Dart. Arrays are represented using Lists in Dart.

A list is a collection of elements of the same data type. An element in a list can be accessed using its index number.

array in dart

My_array is a variable that represents an array, which is a collection of items. This specific collection has 10 and 14 stored in it. An index is used to reference each element in the array. The index value starts at zero to represent the first element and goes all the way up to n-1, where n is the last element in the array. Thus, for an array with size 5, the index will range from 0 to 4.


Dart data types in dart dart keywords number data type in dart string data type in dart boolean data type in dart list data type in dart map data type in dart dynamic data type in dart constant data type in dart array in dart
Blogs