Skip to main content
almarefa.net

Back to all posts

How to Declare A Variable In Dart?

Published on
4 min read
How to Declare A Variable In Dart? image

Best Dart Programming Guides to Buy in October 2025

1 Flutter Design Patterns and Best Practices: Build scalable, maintainable, and production-ready apps using effective architectural principles

Flutter Design Patterns and Best Practices: Build scalable, maintainable, and production-ready apps using effective architectural principles

BUY & SAVE
$20.60 $44.99
Save 54%
Flutter Design Patterns and Best Practices: Build scalable, maintainable, and production-ready apps using effective architectural principles
2 Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud

BUY & SAVE
$34.39 $65.99
Save 48%
Flutter and Dart Cookbook: Developing Full-Stack Applications for the Cloud
3 Dart Programming Language, The

Dart Programming Language, The

BUY & SAVE
$37.67
Dart Programming Language, The
4 Dart in Action

Dart in Action

BUY & SAVE
$38.85 $44.99
Save 14%
Dart in Action
5 Dart Programming, In 8 Hours, For Beginners, Learn Coding Fast: Dart Programming Language, Crash Course Tutorial, Quick Start Guide & Exercises

Dart Programming, In 8 Hours, For Beginners, Learn Coding Fast: Dart Programming Language, Crash Course Tutorial, Quick Start Guide & Exercises

BUY & SAVE
$13.99
Dart Programming, In 8 Hours, For Beginners, Learn Coding Fast: Dart Programming Language, Crash Course Tutorial, Quick Start Guide & Exercises
6 Flutter in 7 Days: Build user-friendly apps with widgets and navigation (English Edition)

Flutter in 7 Days: Build user-friendly apps with widgets and navigation (English Edition)

BUY & SAVE
$31.62 $34.95
Save 10%
Flutter in 7 Days: Build user-friendly apps with widgets and navigation (English Edition)
+
ONE MORE?

In Dart, you can declare a variable using the var keyword followed by the variable name and an optional type annotation. Here's the syntax:

var variableName; // Declaring a variable without assigning a value var variableName = value; // Declaring a variable and assigning a value var variableName1, variableName2; // Declaring multiple variables in one line

You can also specify the type explicitly using type annotations:

TypeName variableName = value; // Declaring a variable with a specified type

Here's an example:

var name = 'John'; // Declaring a variable 'name' and assigning 'John' as its value int age = 25; // Declaring an integer variable 'age' and assigning 25 as its value double height = 1.75; // Declaring a double variable 'height' and assigning 1.75 as its value bool isStudent = true; // Declaring a boolean variable 'isStudent' and assigning true as its value

In Dart, variables are dynamically typed by default, meaning their types can change during runtime. However, you can specify the type explicitly to improve code readability and catch potential type errors.

Remember that Dart provides several built-in types like int, double, bool, String, etc., and you can also create custom types using classes or other mechanisms.

How to declare and initialize a variable with a nullable data type in Dart?

To declare and initialize a variable with a nullable data type in Dart, you can use the following syntax:

DataType? variableName = initialValue;

For example, if you want to declare and initialize a variable of type String that can hold a null value, you can do it like this:

String? name = null;

The ? after the data type (String) indicates that the variable can hold a null value. If you don't assign an initial value, the default value will be null.

How to declare and initialize a variable with a default value in Dart?

To declare and initialize a variable with a default value in Dart, you can use the = operator in the variable declaration.

Here's an example:

// Declare and initialize a variable with a default value int myVariable = 0;

// Alternatively, you can use the "var" keyword to let Dart infer the type var myVariable = 0;

In the above example, the variable myVariable is declared and initialized with a default value of 0. The type of the variable is explicitly mentioned using int, but you can also use the var keyword to let Dart automatically infer the type based on the value.

You can change the default value to whatever you need based on the type of the variable.

How to declare and initialize a variable using the late keyword in Dart?

In Dart, the late keyword is used to declare a variable that will be initialized at a later point in the code. The late keyword is useful when you need to declare a non-nullable variable but you are not able to assign a value to it immediately.

Here's an example of how to declare and initialize a variable using the late keyword:

late String name;

void main() { // Assign a value to the 'name' variable later in the code someFunction();

// Use the 'name' variable print(name); }

void someFunction() { // Initialize the 'name' variable name = 'John Doe'; }

In the example above, the name variable is declared using the late keyword, indicating that its value will be assigned later in the code. The someFunction is called later on to initialize the name variable with the value "John Doe". Finally, the initialized name variable is printed to the console.

Note: It's important to initialize the late variable before accessing its value, otherwise, Dart will throw a LateInitializationError.