Variables are used to store data in a program. In Dart, there are multiple ways to declare a variable depending on your requirements.
Let's explore the most common ways.
π 1. Declare Variables with Explicit Data Types
Use the data type when you know exactly what kind of value the variable will store.
String name = "Tuhin";
int age = 25;
double height = 5.8;
bool isFlutterDeveloper = true;
β This approach makes your code more readable and type-safe.
π 2. Declare Variables with var
The var keyword lets Dart automatically determine the variable's type.
var city = "Dhaka";
var year = 2026;
After the first value is assigned, the variable's type cannot change.
var city = "Dhaka";
city = "Khulna"; // β
Valid
// city = 100; // β Error
π 3. Declare Variables with dynamic
Use dynamic when a variable may store different types of values.
dynamic value = "Flutter";
value = 100;
value = true;
The variable can change its type during program execution.
π 4. Declare Variables with final
A final variable can be assigned only once.
final String country = "Bangladesh";
final currentYear = 2026;
Once a value is assigned, it cannot be changed.
π 5. Declare Variables with const
Use const for values that are known at compile time and will never change.
const double pi = 3.14159;
const appName = "Flutter Learning";
π‘ Best Practice
- β Use explicit data types when the type should be clear.
- β
Use
varwhen the type is obvious from the assigned value. - β
Use
finalfor values that should not change after initialization. - β
Use
constfor compile-time constant values. - β οΈ Use
dynamiconly when different data types are truly required.
Writing variables correctly is one of the first steps toward writing clean, maintainable, and professional Dart code.
π¬ Which way do you prefer to declare variables in Dart?

Top comments (0)