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)