Data Types in Dart
Data type means the type of data that can be represented and manipulated in a programming language.
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:
int - Represents integers. (e.g.,
int age = 25;
).double - Represents floating-point numbers. (e.g.,
double price = 19.99;
).String: Represents a sequence of characters. (e.g.,
String name = "John";
).bool - Represents a Boolean value. (
true
orfalse
, 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.
List - Represents a collection of variables. (Array). (e.g.,
List<int> numbers = [1, 2, 3];
).Map - Represents a collection of key-value pairs. (e.g.,
Map<String, dynamic> person = {'name': 'Alice', 'age': 30};
).Set - Represents an unordered collection of unique values. (e.g.,
Set<String> colors = {'red', 'green', 'blue'};
).Symbol - Represents an operator or identifier. (e.g.,
Symbol mySymbol = #example;
).Function - You can also use data types to represent functions. (e.g.,
Function myFunction = () => print('Hello, Dart!');
).Runes - Represents a sequence of Unicode characters. (e.g.,
Runes heart = '\u2665';
).null - Represent the absence of a value.
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.
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.
Jai Hind, Vande Mataram 🇮🇳