DART์—์„œ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•?

์ž‘์„ฑ์ž

์นดํ…Œ๊ณ ๋ฆฌ:

โ† ํ”ผ๋“œ๋กœ
DEV Community ยท M R Tuhin ยท 2026-07-03 ๊ฐœ๋ฐœ(SW)

M R Tuhin

๐Ÿ’™ Dart Basics: How to Declare Variables in Dart

Variables are used to store data in a program. Every Flutter developer works with variables every day, so understanding them is an important first step in learning Dart.

๐Ÿ“Œ 1. Declare a Variable with a Data Type

When you already know the type of data, declare it explicitly.

String name = "Tuhin";
int age = 22;
double height = 5.8;
bool isStudent = true;

Enter fullscreen mode Exit fullscreen mode

โœ… This makes your code easier to read and maintain.

๐Ÿ“Œ 2. Using var

The var keyword tells Dart to automatically detect the variable’s type from the first value you assign.

var city = "Dhaka";
var year = 2025;

print(city);
print(year);

Enter fullscreen mode Exit fullscreen mode

Once the type is assigned, it cannot be changed.

var city = "Dhaka";
city = "Rajshahi";   // โœ… Correct
// city = 100;        // โŒ Error

Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ 3. Using dynamic

The dynamic keyword allows a variable to hold values of different data types.

dynamic data = "Hello";
print(data);

data = 100;
print(data);

Enter fullscreen mode Exit fullscreen mode

Another example:

dynamic value = true;
value = 3.14;
value = "Flutter";

Enter fullscreen mode Exit fullscreen mode

This works because dynamic does not enforce a fixed data type.

๐Ÿ” var vs dynamic

Feature var dynamic Type Inference โœ… Yes โŒ No fixed type Type Changes โŒ Not Allowed โœ… Allowed Type Safety โœ… Strong โŒ Weak Performance โœ… Better โš ๏ธ Slightly less efficient Best Use When the type is known When different types are required

๐Ÿ’ก Best Practice

  • โœ… Use explicit data types for better readability.
  • โœ… Use var when the type is obvious.
  • โš ๏ธ Use dynamic only when you really need to store different types of values.

Choosing the right variable type helps you write cleaner, safer, and more maintainable Dart code.

๐Ÿ’ฌ Which one do you use most in your Flutter projectsโ€”var or dynamic?

Flutter #Dart #FlutterDeveloper #Programming #MobileDevelopment #SoftwareEngineering

์›๋ฌธ์—์„œ ๊ณ„์† โ†—

์ถ”์ถœ ๋ณธ๋ฌธ ยท ์ถœ์ฒ˜: dev.to ยท https://dev.to/m_rtuhin/how-to-declare-variables-in-dart–545l

์ฝ”๋ฉ˜ํŠธ

๋‹ต๊ธ€ ๋‚จ๊ธฐ๊ธฐ

์ด๋ฉ”์ผ ์ฃผ์†Œ๋Š” ๊ณต๊ฐœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•„์ˆ˜ ํ•„๋“œ๋Š” *๋กœ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค