How to Declare A Variable In Dart?

8 minutes read

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:

1
2
3
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:

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


Here's an example:

1
2
3
4
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.

Best Dart Books to Read in 2024

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

Rating is 5 out of 5

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

2
Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

Rating is 4.9 out of 5

Flutter Cookbook: Over 100 proven techniques and solutions for app development with Flutter 2.2 and Dart

3
Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

Rating is 4.8 out of 5

Quick Start Guide to Dart Programming: Create High-Performance Applications for the Web and Mobile

4
Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

Rating is 4.7 out of 5

Dart: Up and Running: A New, Tool-Friendly Language for Structured Web Apps

5
The Dart Programming Language

Rating is 4.6 out of 5

The Dart Programming Language

6
Mastering Dart: A Comprehensive Guide to Learn Dart Programming

Rating is 4.5 out of 5

Mastering Dart: A Comprehensive Guide to Learn Dart Programming

7
Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

Rating is 4.4 out of 5

Flutter Cookbook: 100+ step-by-step recipes for building cross-platform, professional-grade apps with Flutter 3.10.x and Dart 3.x, 2nd Edition

8
Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition

Rating is 4.3 out of 5

Flutter for Beginners: An introductory guide to building cross-platform mobile applications with Flutter 2.5 and Dart, 2nd Edition


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:

1
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:

1
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:

1
2
3
4
5
// 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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.

Facebook Twitter LinkedIn Whatsapp Pocket

Related Posts:

Dart DevTools is a powerful tool for debugging Dart applications. It provides a comprehensive set of features to help you understand and analyze the behavior of your code during runtime. Here's an overview of how to use Dart DevTools for debugging:Installa...
To send emails via Gmail from Dart, you can use the mailer package. Here are the steps to achieve this:Set up your Dart project by creating a new Dart file and importing the necessary packages. import 'package:mailer/mailer.dart'; import 'package:m...
To invoke Dart code from Kotlin code, you can use the platform channel provided by Flutter. This allows communication between the Dart and Kotlin code in your app.First, define a method in your Dart code that you want to invoke from Kotlin. Then, create a Meth...