Variables are fundamental components in programming, serving as containers that hold values. In Dart, variables allow you to store data that can be manipulated and retrieved throughout your program.
What is a Variable?
A variable is a named storage location in a computer's memory where data can be stored. You can think of a variable as a labeled box in which you can keep different types of data.
Example:
// Here the variable 'name' contains the value "John".
var name = "John";
Variable Types
Variables can hold different kinds of values, which are determined by their data types. Here are the primary data types in Dart:
- String: Used for storing text values.
- Example:
"John"
(must be enclosed in quotes)
- Example:
- int: Used for storing integer values (whole numbers).
- Example:
10
,-10
,8555
(decimal values are not included)
- Example:
- double: Used for storing floating-point values (numbers with a decimal point).
- Example:
10.0
,-10.2
,85.698
(includes decimal values)
- Example:
- num: A general type for numbers that can be either
int
ordouble
.- Example:
10
,20.2
,-20
- Example:
- bool: Used for storing boolean values, which can be either
true
orfalse
.- Example:
true
,false
(only stores these two values)
- Example:
- var: A flexible type that can store any value, with the type inferred at runtime.
- Example:
var mixedValue = 'Bimal';
,var age = 12;
,var isAlive = true;
- Example:
Syntax for Declaring Variables
The syntax for creating a variable in Dart is straightforward:
type variableName = value;
Example 1: Using Variables in Dart
In this example, you will learn how to declare variables, assign values to them, and print those values to the console.
void main() {
// Declaring variables
String name = "John"; // A string variable
int age = 30; // An integer variable
double height = 5.9; // A double variable
bool isStudent = false; // A boolean variable
// Printing variable values
print('Name: $name'); // Output: Name: John
print('Age: $age'); // Output: Age: 30
print('Height: $height'); // Output: Height: 5.9
print('Is Student: $isStudent'); // Output: Is Student: false
}
Explanation of the Code
- Variable Declaration: Each variable is declared with a specific type followed by its name and an optional initial value.
- String Interpolation: In the
print
statements, the$
symbol is used to embed variable values directly within the strings.
Variable Naming Conventions
Using consistent naming conventions is essential for writing clean, readable code. In Dart, the following conventions are commonly used:
- Lower Camel Case: For variable names, start with a lowercase letter and capitalize subsequent words.
- Example:
studentName
,totalAmount
,isActive
- Example:
- Descriptive Names: Variable names should be descriptive enough to convey their purpose.
- Good Example:
userAge
,itemCount
- Poor Example:
x
,temp
- Good Example:
- Avoid Special Characters: Use only letters, numbers, and underscores. Avoid spaces and special characters.
- Valid Example:
first_name
- Invalid Example:
first-name
,first name
- Valid Example:
Variable Scope
Variable scope determines where a variable can be accessed or modified within your code. There are two primary types of scope in Dart:
Local Variables:
- Declared within a function or block and can only be accessed within that function or block.
- Example:
void greet() { var message = 'Hello, World!'; // Local variable print(message); } // message cannot be accessed here
Global Variables:
- Declared outside of any function and can be accessed from anywhere in the file.
- Example:
var globalMessage = 'I am global!'; // Global variable void displayMessage() { print(globalMessage); // Accessible here }
Conclusion
Understanding variables is crucial for effective programming in Dart. You've learned about different types of variables, how to declare them, naming conventions, and variable scope. As you continue your Dart programming journey, mastering variables will significantly enhance your ability to manage and manipulate data in your applications.
PLAY QUIZ