Data Types in Dart

Data Types in Dart

Data type means the type of data that can be represented and manipulated in a programming language.

·

4 min read

Dart supports mainly nine types of data type. They are represented in a very simple and concise manner. Data types can be categorized into two main groups: primitive types and non-primitive types (also known as reference types). Let's explore both categories:

Primitive Data Types in Dart

Primitive types in Dart are basic data types that represent single values. They are immutable, and their values are stored directly in memory. Dart provides several primitive types, including:

  1. int - Represents integers. (e.g., int age = 25;).

  2. double - Represents floating-point numbers. (e.g., double price = 19.99;).

  3. String: Represents a sequence of characters. (e.g., String name = "John";).

  4. bool - Represents a Boolean value. (true or false, e.g., bool isStudent = true;).

Example

int age = 25; // age is an integer with a value of 25
age = 30; // Now age is 30, and the old value (25) is replaced.
String name = "John"; // name is a string with the value "John"
name = "Alice"; // Now name is "Alice," and the old value ("John") is replaced.

When you assign a new value to a variable of a primitive type, it replaces the previous value.

Non-Primitive Data Types in Dart

Non-primitive types represent more complex data structures and store references to the data. When you assign a non-primitive variable to a new value, you change the reference to the data, not the data itself.

  1. List - Represents a collection of variables. (Array). (e.g., List<int> numbers = [1, 2, 3];).

  2. Map - Represents a collection of key-value pairs. (e.g., Map<String, dynamic> person = {'name': 'Alice', 'age': 30};).

  3. Set - Represents an unordered collection of unique values. (e.g., Set<String> colors = {'red', 'green', 'blue'};).

  4. Symbol - Represents an operator or identifier. (e.g., Symbol mySymbol = #example;).

  5. Function - You can also use data types to represent functions. (e.g., Function myFunction = () => print('Hello, Dart!');).

  6. Runes - Represents a sequence of Unicode characters. (e.g., Runes heart = '\u2665';).

  7. null - Represent the absence of a value.

  8. dynamic - Represents a variable whose type is not known at compile time.

Example

List<int> numbers = [1, 2, 3]; // numbers is a list of integers.
numbers.add(4); // You modify the list by adding a new number (4).
Map<String, dynamic> person = {'name': 'Alice', 'age': 30};
person['age'] = 31; // You modify the map by changing the 'age' value to 31.

In summary, primitive types store values directly, and when you change the value, you replace the old value. Non-primitive types store references to data structures, and when you change them, you modify the data structure itself. Understanding this difference is crucial when working with Dart, especially in cases where you need to manage and manipulate data structures.

💡
NOTE:- Although Dart is strongly typed, type annotations are optional because Dart can infer types.

That’s all you need to know about data types in the dart. Feel free to let me know if I missed something. I’d love to learn that.

Till then Keep Loving, Keep Coding.

Remember no teacher, no book, no video tutorial, or no blog can teach you everything. As one said Learning is Journey and Journey never ends. Just collect some data from here and there, read it, learn it, practice it, and try to apply it. Don’t feel hesitant that you can’t do that or you don’t know this concept or that concept. Remember every programmer was passed from the path on which you are walking right now. Remember Every Master was Once a Beginner. Work hard and Give your best.

💡
I have written a different article for Map and List. Check them as well for more information on Data types. Subscribe to My Newsletter For More Such Content...

Jai Hind, Vande Mataram 🇮🇳

Learn More about Dart and Flutter

Follow me for more such content

Did you find this article valuable?

Support Jay Tillu by becoming a sponsor. Any amount is appreciated!