What are Variables in Dart?
A variable is a named storage location in memory that holds a value. In Dart, variables can store different types of data such as numbers, text, booleans, and more. Dart is a statically typed language, but it also supports type inference, making variable declaration flexible and concise.
Declaring Variables
You can declare a variable using the var keyword, or by explicitly specifying the type. Once a variable is declared with a type, it can only hold values of that type.
Built‑in Data Types
Dart provides several core data types:
- int: Integer numbers (e.g.,
42,-10)
- int: Integer numbers (e.g.,
- double: Floating‑point numbers (e.g.,
3.14,-0.5)
- double: Floating‑point numbers (e.g.,
- num: Generic number type (can be int or double)
- String: Sequence of characters (
'Hello',"Dart")
- String: Sequence of characters (
- bool: Boolean values (
trueorfalse)
- bool: Boolean values (
- List: Ordered collection (similar to arrays)
- Map: Key‑value pairs
- dynamic: Can hold any type (disables type checking)
Type Inference with var
When you declare a variable with var, Dart infers its type from the initial value. After inference, the variable's type is fixed and cannot be changed.
Explicit Type Declaration
You can also specify the type explicitly. This is useful when you want to be clear about the intended type or when you declare a variable without an initial value.
The dynamic Type
If you need a variable that can change type at runtime, use dynamic. However, overusing dynamic defeats the purpose of static type checking and should be avoided unless absolutely necessary.
Final and Const – Immutable Variables
Use final or const for variables that should not change after being set. final is set once at runtime; const is a compile‑time constant.
Null Safety in Dart
Dart supports sound null safety. By default, variables cannot be null. To allow a variable to hold null, add a ? after the type.
Default Values
Uninitialized variables with a nullable type have the default value null. For non‑nullable types, you must provide an initial value before use.
Type Conversion
You can convert between types using built‑in methods like toString(), parse(), etc.
Naming Conventions
- Variable names should use camelCase (e.g.,
firstName,totalAmount).
- Variable names should use camelCase (e.g.,
- Class names use PascalCase (e.g.,
Person,ShoppingCart).
- Class names use PascalCase (e.g.,
- Constants (with
const) often use lowerCamelCase (no special prefix).
- Constants (with
- Avoid using single‑letter names except for trivial loop counters (
i,j).
- Avoid using single‑letter names except for trivial loop counters (
- Names should be descriptive and meaningful.
Complete Example
Key Takeaways
- Variables store data and have a type.
- Use
varfor type inference, or explicitly specify the type.
- Use
- Dart has built‑in types:
int,double,String,bool,List,Map.
- Dart has built‑in types:
- Use
finalfor runtime constants andconstfor compile‑time constants.
- Use
- Null safety prevents null errors – use
?for nullable types.
- Null safety prevents null errors – use
- Always follow naming conventions for clean code.