You are currently viewing Dart Data Types

Dart Data Types

Dart, developed by Google, is a versatile programming language designed for building web, mobile, and server applications. One of the fundamental aspects of Dart programming is the concept of data types. Data types help define the kind of values that variables can hold and the operations that can be performed on them. In this article, we will explore various data types in Dart, covering primitive types, integers (both signed and unsigned), as well as var, dynamic, and null.

Integers

Signed Integers

Dart supports both signed and unsigned integers. Signed integers can represent both positive and negative values. Let’s look at some examples:

void main() {

    int positiveInteger = 42;
    int negativeInteger = -17;

    print("Positive Integer: $positiveInteger");
    print("Negative Integer: $negativeInteger");

}

In the example above, we’ve declared two integers, positiveInteger and negativeInteger, representing positive and negative values, respectively.

Dart also supports hexadecimal literals, making it convenient for developers who prefer working with base-16 numbers.

void main() {

    int hexVariable = 0xDEADBEEF;
    print("Hex Variable: $hexVariable");

}

The $ symbol within the string is used for string interpolation, allowing us to embed the variable values directly into the printed text.

Unsigned Integers

While Dart does not have a separate data type explicitly labeled as “unsigned integer,” you can achieve the same effect using the int data type. Dart’s int can represent both positive and negative values, but it is important to note that there is no enforced boundary for non-negative integers.

BigInt

Dart’s BigInt type is used for representing arbitrary-precision integers:

void main() {

  BigInt bigInteger = BigInt.from(9223372036854775807) * BigInt.two;
  print("BigInt: $bigInteger");
  
}

Here, we create a BigInt by multiplying the maximum 64-bit signed integer by 2.

Floating-Point Numbers

Dart supports both double and num types for floating-point numbers. The double type represents 64-bit floating-point numbers.

void main() {

    double doubleValue = 3.14;
    print("Double Value: $doubleValue");
	
}

The num type is a generic numeric type that includes both integers and floating-point numbers:

void main() {

    num numericValue = 42; // Integer
    print("Numeric Value: $numericValue");

    numericValue = 3.14; // Floating-point
    print("Numeric Value: $numericValue");

}

Booleans

Booleans in Dart are represented by the bool data type. A boolean can only have two values: true or false. Let’s see how booleans work in Dart:

void main() {

    bool isDartFun = true;
    bool isLearningInProgress = false;

    print("Is Dart Fun? $isDartFun");
    print("Is Learning in Progress? $isLearningInProgress");

}

Here, isDartFun is assigned the value true, indicating that Dart is considered fun. On the other hand, isLearningInProgress is assigned false, suggesting that the learning process is not currently underway.

Strings

Strings in Dart are sequences of characters, and they are represented by the String data type. Dart supports both single and double quotes for defining strings:

void main() {

    String greeting = 'Hello, Dart!';
    String message = "Welcome to the world of programming.";

    print(greeting);
    print(message);
    
}

Both greeting and message are examples of string variables, containing text data.

Var

The var keyword is used to declare variables with an inferred type. Dart infers the type based on the assigned value.

void main() {

    var varVariable = 42;
    print("Var Variable: $varVariable");

    var greetingMessage = 'Hello, Dart!';
    print("Greeting Message: $greetingMessage");

}

In the above examples, Dart will automatically infer that varVariable is of type int and greetingMessage is of type String.

Dynamic

Dart also includes a dynamic data type, which is flexible and can change its type during runtime. While it provides versatility, it is essential to use it judiciously to maintain code clarity.

void main() {

    dynamic dynamicVariable = 42;
    print("Dynamic Variable: $dynamicVariable");

    dynamicVariable = "Now I'm a String!";
    print("Dynamic Variable: $dynamicVariable");

}

In the example above, dynamicVariable starts as an integer but later transforms into a string. While using dynamic provides flexibility, it comes at the cost of losing some of the benefits of static typing, such as early error detection.

Handling Null

Dart supports null safety, ensuring variables cannot contain null unless explicitly declared nullable. Let’s explore how null works:

void main() {

    String? nullableString = "I can be null";
    print("Nullable String: $nullableString");

    // Without the question mark (?) after the String data type, null assignment fails
    nullableString = null;
    print("Nullable String: $nullableString");

}

In this example, nullableString is declared as nullable with the ? symbol, allowing it to be assigned a null value later.

Dart introduces the null-aware operators (??, ?., and ??=) to handle null values more efficiently.

void main() {

    int? nullableNumber;
    int defaultValue = 42;
    
    int result = nullableNumber ?? defaultValue;
    print('Result: $result');

}

The ?? operator checks if the value on its left is null; if so, it uses the value on the right as a default.

void main() {

    String? maybeNullString;

    int length = maybeNullString?.length ?? 0;
    print('String Length: $length');
    
}

Here, the ?. operator checks if maybeNullString is null before attempting to access its length. If it’s null, the ?? operator provides a default value of 0.

The ??= operator is used to assign a value only if the variable is currently null.

void main() {

    int? nullableVariable;
    int defaultValue = 42;

    nullableVariable ??= defaultValue;
    print('Nullable Variable: $nullableVariable');
    
}

Conclusion

Understanding Dart data types is essential for writing robust and efficient code. From primitive types to dynamic typing and null handling, Dart provides a versatile set of tools to work with different kinds of data. As you continue your journey with Dart, mastering these data types will empower you to build sophisticated and reliable applications.

Sources:

Leave a Reply