Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Learn Dart Varibles

In Dart, variables are used to store and manipulate data. Dart is a statically typed language, which means that variables have types, and once a variable is declared with a certain type, it cannot hold values of any other type.



Types of Dart Varibles

1. var : This keyword is used for declaring variables where you want Dart to infer the type. For example

var name = "John"; // Dart infers that name is a String
var age = 30; // Dart infers that age is an int


2. Dynamic : If you're unsure about the type of a variable or if it can change its type during runtime, you can use the dynamic keyword. for example

dynamic dynamicVariable = "Hello"; // Can hold any type of value
dynamicVariable = 10; // Now it's an int


3. Final : Declaring a variable as final means that once it's initialized, its value cannot be changed. However, the value itself can be mutable (like a list or a map). dart. for example-

final int x = 10;



4. Const : Constants are declared using the const keyword. They are compile-time constants, meaning their values must be known at compile-time. This is different from final, which can be set at runtime but cannot change once set.for example

const double PI = 3.14;



Here's an example demonstrating the usage of variables in Dart:

void main() {
var name = "Alice";
var age = 25;
print("Name: $name, Age: $age");
dynamic dynamicVariable = "Hello";
print(dynamicVariable); // Outputs: Hello
dynamicVariable = 10;
print(dynamicVariable); // Outputs: 10
final int x = 10;
// x = 20; // This will cause an error since x is final
const double PI = 3.14;
print(PI); // Outputs: 3.14
}






Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java