Dart Programming (Variables in Dart)
Variables in Dart
In Dart, variables are used to store and manipulate data. Dart is a statically typed language, which means that each variable has a declared type that is known at compile-time. Here are the key aspects of variables in Dart:
1. Variable Declaration:
To declare a variable in Dart, you use the `var`, `final`, or `const` keyword followed by the variable's name and an optional initializer (a value assigned to the variable when it's declared). For example:
var name = 'John'; // var declares a variable with type inference
final age = 30; // final declares an immutable variable
const pi = 3.14; // const declares a compile-time constant
2. Type Inference:
Dart can infer the type of a variable based on its initializer. You can use `var` for type inference, and the variable's type will be determined from the assigned value. Once the type is inferred, it cannot be changed.
var number = 42; // Inferred type: int
var message = 'Hi'; // Inferred type: String
3. Final Variables:
Variables declared as `final` cannot be reassigned after their initial value is set. They are typically used for values that should not change during the program's execution.
final name = 'Alice';
4. Compile-Time Constants:
Variables declared as `const` are compile-time constants. They must be initialized with a value that is known at compile-time and cannot be changed. `const` variables are implicitly `final`.
const int daysInWeek = 7;
5. Explicit Type Annotation:
You can explicitly specify the data type of a variable using the type annotation. This is useful when you want to be explicit about the variable's type or when type inference might not provide the desired type.
String greeting = 'Hello'; // Explicitly specifying the type as String
6. Dynamic Type:
Dart also has a `dynamic` type that allows a variable to hold values of any type. This provides flexibility but sacrifices type safety.
dynamic dynamicVariable = 42; // Can hold any type
dynamicVariable = 'Dart'; // Now it holds a String
7. Late Variables (Null Safety):
In Dart's null safety feature, you can use `late` to declare a variable that is initially uninitialized but will be assigned a value before it's used. This allows null safety while delaying initialization.
late String lateVariable;
lateVariable = 'Initialized later';
// Variable's
import 'dart:io';
void main()
{
var name = "Karthikeyan A";
stdout.write("Please enter your age: "); // get the input in the same line.
final int age = int.parse(stdin.readLineSync()!);
dynamic myThings;
myThings = ["Dart", "JAVA", "Flutter"];
print("\n\n\tAbout me");
print("My name is ${name}");
print("My age is ${age}");
const String interest = "Dart Programming and Flutter Dev.";
print("my Interest is ${interest}");
print("My Skills are ${myThings}");
myThings = 2021;
print("I started my Computer Science journey in ${myThings}");
late String lateVariable; // Declare a late variable
print("Before assignment");
// Simulate some logic that assigns a value to lateVariable
Future.delayed(Duration(seconds: 2), () {
lateVariable = 'Initialized later';
print("Value assigned to lateVariable");
});
// Attempting to use lateVariable before it's assigned would result in a runtime error
// Uncommenting this line would lead to a late initialization error:
// print("Late variable value: $lateVariable");
// Wait for the value to be assigned using a Future
Future.delayed(Duration(seconds: 3), () {
print("Late variable value: $lateVariable");
});
}
Comments
Post a Comment