Variables In Dart Language – Dart Programming Variables Guide

Haris Bin Nasir Avatar

·

·

In Dart, you store data in variables that you can reference and manipulate throughout your program. Dart enforces strong typing, ensuring that every variable has a specific type. However, you can also benefit from Dart’s support for type inference, which allows you to skip explicitly declaring a type if the compiler can determine it based on the assigned value.

When you work with Dart, understanding how to use variables effectively is essential. By leveraging Dart’s type system and inference capabilities, you can write cleaner and more maintainable code. Whether you’re dealing with simple data types or more complex structures, mastering variable declarations and usage in Dart will significantly enhance your programming efficiency.

For further information on detailed topics of Dart, click here.

Declaring Variables

There are different ways to declare variables in Dart:

  • Using var:
    The var keyword allows Dart to automatically infer the type of the variable based on the assigned value. For example:
var name = "Alice";  // Dart infers that 'name' is a String
var age = 25;        // Dart infers that 'age' is an int
  • Using Explicit Types:
    You can explicitly declare the type of a variable for clarity or when type inference is not sufficient. For example:
String city = "New York";
int population = 8000000;
  • Using final:
    The final keyword is used for variables whose value is set once and cannot be changed (immutable). It is similar to a constant, but the value can be set at runtime:
final country = "USA";  // The value of 'country' cannot be changed later
  • Using const:
    The const keyword defines compile-time constants. You assign these variables a value that you know and cannot change during the runtime of the program:
const pi = 3.14159;  // The value of 'pi' is a constant known at compile time

Variable Types

Dart supports various data types for variables:

  • int:
    Used to store integer values:
int count = 10;
  • double:
    Used for floating-point numbers (decimal values):
double price = 9.99;
  • String:
    Used for textual data:
String greeting = "Hello, World!";
  • bool:
    Used for Boolean values (true or false):
bool isActive = true;
  • Null:
    Represents the absence of a value:
Null noValue = null;

Dart also supports more complex data types, such as lists, maps, sets, and custom objects, which you can use to store collections of values or more complex structures.

Mutable vs. Immutable Variables

  • Mutable variables can be modified after they are declared. For example, variables declared with var or with an explicit type are mutable:
var counter = 1;
counter = 2;  // This is allowed
  • Immutable variables cannot be modified after their initial assignment. Variables declared with final or const are immutable:
final score = 100;
// score = 90;  // This will cause an error

Conclusion

In Dart, variables play a fundamental role in storing and manipulating data. Understanding how to declare, initialize, and use variables effectively is key to mastering the language. Whether you’re working with basic data types like int, double, and String, or more complex collections and objects, Dart provides flexible and powerful options for managing data. By familiarizing yourself with mutable and immutable variables, as well as the use of var, final, and const, you’ll be better equipped to write efficient and maintainable code in Dart.

Happy Coding…!!!

Leave a Reply

Your email address will not be published. Required fields are marked *