Introduction of Dart Variable Types
In Dart, the programming language used by Flutter, Dart Variable Types refer to the way variables store references to values. Dart is type-safe, meaning that the type of value a variable can hold is identified either explicitly or inferred by the Dart compiler. Here’s a rundown of the types of variables you can use in Dart along with a brief description and example for each:
1. int – Integer
Stores whole numbers without a decimal point.
eg: int age = 30;
2. double – Floating-Point Number
Stores numbers with a decimal point, useful for more precise calculations.
eg: double price = 5.99;
3. String – Text
Stores sequences of characters, commonly used to represent words, sentences, or any form of text.
eg: String name = “John Doe”;
4. bool – Boolean
Stores boolean values, which can only be either true or false.
eg: bool isVisible = true;
5. List – Collection of Ordered Objects
A list is a collection of ordered objects, similar to arrays in other programming languages.
eg: List<int> numbers = [1, 2, 3, 4, 5];
6. Map – Key-Value Pairs
Stores objects in a key-value format, similar to dictionaries in other languages.
eg: Map<String, dynamic> user = {
“name”: “Alice”,
“age”: 25,
“isSubscribed”: true
};
7. Set – Collection of Unique Items
A set is a collection of unique items that do not repeat.
eg: Set<String> colors = {‘red’, ‘green’, ‘blue’};
8. var – Inferred Type Variable
Declares a variable without specifying its type, which will be inferred by the compiler based on the value it is assigned.
eg: var email = “example@example.com”; // Inferred as String
9. dynamic – Any Type
Declares a variable that can hold any type of value, changing type as needed.
eg: dynamic anything = “I can be anything!”;
anything = 123; // Now it’s an int
anything = false; // Now it’s a bool
10. final – Immutable Variable
Declares a variable that can only be set once and cannot be changed thereafter.
eg: final birthDate = DateTime(1990, 1, 1);
11. const – Compile-Time Constant
Declares a variable whose value is a compile-time constant, meaning it’s determined during compilation and cannot change.
eg: const pi = 3.14;
Conclusion Of Dart Variable Types
Understanding these different dart variable types to enhances your ability to manage data effectively within your Flutter applications. It allows for a robust and dynamic approach to coding, accommodating a wide range of functionalities and user experiences.
Thank for visiting Hybrid App Development
Posted by Hussam HM