Table of contents
1.
Introduction
2.
Boolean in Dart
3.
Constants in Dart
3.1.
Const
3.2.
Final in Dart
4.
Frequently Asked Questions
4.1.
How does the compiler understand the keywords?
4.2.
Can we use final and const without specifying the data types?
4.3.
Can we use 0 and 1 in boolean variables?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Constants and Boolean in Dart

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will be learning about the boolean data type. Apart from the basic data types, dart also provides us with the boolean data type, which can take only two types of values true and false. To learn about the other data types, go to this link. Along with the boolean data type, will we also discuss the const keyword in dart with the relevant examples.

Boolean in Dart

As discussed above, bool type variables can take only true or false values. Decision control statements mainly use bool variables. In dart, it cannot take the integers such as 1 or 0 only takes true or false, which are compile-time constants.

Syntax:

bool var1 = true;
Bool var2 = false;

 

Example: This example shows the declaration and initialization of boolean variables.
Code:

void main() {
  bool var1 = true;
  bool var2 = false;
  print(var1);
  print(var2);
}


Output:

true
false

Explanation: Variables ‘var1’ and ‘var2’ are of type boolean so they can only take true or false as their values.

 

Example: In this example, we will assign values to bool variables using the result of a conditional statement.
Code:

void main() {
  int a=100, b=200;
  //Assigning the values to bool variables.
  bool var1 = a > b;
  bool var2 = a < b;
  print(var1);
  print(var2);
}


Output:

false
true

Explanation: Here we use conditional statements for the initialization of bool variables. The result of a > b is false therefore false is assigned to ‘var1’ and result of a < b is true therefore true is assigned to ‘var2’.

 

Example: In this example, we use bool variables as conditional statements.
Code:

void main() {
  bool getInside = true;
  bool notGetInside = false;
  //Use of bool in control statements
  //Use true in this condition.
  if(getInside)
    print("I am inside 1st If stat.");
  
  //use false in this condition.
  if(notGetInside)
    print("I am inside 2nd If stat.");
}


Output:

I am inside 1st If stat.

Explanation: Here we use our bool variables as a conditional statement for ‘if' blocks.

 

Example: Here we use the bool variable as a flag in our conditional statement.
Code:

void main() { 
  bool flag = true; 
  for(int i=1; (i<=10 & flag); i++)
  {
    print(i);
    if(i==5){
      //blocking the loop.
      flag = false;
    }   
  }
}


Output:

1
2
3
4
5

Explanation: If the flag variable in the for loop becomes false then the for loop will stop working and when the ‘i’ becomes 5 we are turning the flag as false. So, from the next iteration flag's value will be false so the for loop will not work and it will print up to 5 only.

Constants in Dart

As the name suggests, it makes the variable constant, which means we cannot change the value of the constant variable during the program's execution, which we have initialized earlier.

The dart constants can be defined using two ways:

  1. Using const keyword
  2. Using final keyword

Const

You can define a constant variable using const in the following way:

Syntax:

const variableName
OR
const dataType variableName

 

Example: Declaring and initializing a const variable.
Code:

void main() { 
  const int a = 10;
  print(a);
}


Output:

10

 

Example: Example of an uninitialized const variable.
Code:

void main() {
  //Just declare a const varaible
  //Not initialized it.
  const int a;
  int b=3;
  print(b);
  //Shows error
  //Just remove the const keyword then,
  //this snippet will not show any error.
}


Output:

Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:2:13:
Error: The const variable 'a' must be initialized.
  const int a;
            ^
Error: Compilation failed.

Explanation: In the above example we created a const variable 'a' and do not initialize it which is not allowed, the const variable must have some value.

Final in Dart

You can define a constant variable using final in the following way:

Syntax:

final variableName
OR
final dataType variableName

 

Example: Declaring and initializing a final variable.
Code:

void main() {
  final int a = 10;
  print(a);
}


Output:

10

 

Example: Example of an uninitialized final variable.
Code:

void main() {
  //Just declare a final varaible
  //Not initialized it.
  final int a;
  int b=3;
  print(b);
  //This will not show any error
}


Output:

3

Explanation: The difference between the final and const variable is that the const variable's value must be known to the program at compile-time, whereas the final variable's value must be known to the program at run-time. That is why the second example in both the keywords shows subtle behavior.

Frequently Asked Questions

How does the compiler understand the keywords?

Keywords are already defined in the compiler along with their respective functionalities. We have to use them with correct spelling.

 

Can we use final and const without specifying the data types?

Yes, we can use these keywords without specifying the data types because dart is type infer language. This means it can automatically detect the data type after providing value to a variable.

 

Can we use 0 and 1 in boolean variables?

No, we only use true or false in boolean variables in dart.

Conclusion

This article thoroughly discussed the Boolean and Constant in dart and the keywords we write to use them. We discussed how different types of variables could be declared and initialized.

We hope that this blog helped you enrich your knowledge regarding Boolean and Constant in Dart. We can use the concepts of dart in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass