- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Immutability in Dart Programming
Immutability is the ability to remain constant. Whenever we talk about immutability we mention the immutable nature.
In object oriented and functional programming, we make use of the immutable nature of objects a lot. Being immutable means that the state of the object cannot be modified after its creation.
It is a very important topic these days when we talk about front-end development, as there are several occasions and scenarios where we want to maintain the state and the way to do that is to make use of immutability.
In Dart, there are different ways with which we can achieve immutability and sometimes we can enforce immutability on our objects and classes as well.
Making use of the 'const' keyword handles the immutability of the variable or object very well.
It should be noted that the const keyword in dart is very different from that present inside the javascript, even though they use the same syntax.
Let's consider an example, where we are declaring a const in javascript, and then try to reassign it to something else, first reassign the entire object, then reassign some property of it.
Example
Consider the example shown below −
// Define USER as a constant and give it an initial value. const USER = { name: 'Mukul'; } // This will throw an error. USER = {}; // But this will not. USER.name = 'Rahul'; console.log(USER.name);
In the above js code, we can see that when we tried reassigning the USER withsomething else, it returned an error. But, if we assign the name property of the USER object, no error is encountered.
Output
Rahul
It happens because in js, the const keyword is just an immutable binding and not an immutable object.
In Dart, the scenario is entirely different, the const keyword ensures that the object remains immutable in nature.
Example
Consider the example shown below −
void main(){ const user = const {'name': 'Mukul'}; // Static error: "Constant variables cannot be assigned a value". user = {}; // Runtime error: "Unsupported operation: Cannot modify unmodifiable Map". user['name'] = 'Rahul'; }
Output
lib/main.dart:5:3: Error: Can't assign to the const variable 'user'. user = {}; ^^^^ Error: Compilation failed.
- Related Articles
- Comments in Dart Programming
- Constructors in Dart Programming
- Enumerations in Dart Programming
- Functions in Dart Programming
- Inheritance in Dart Programming
- Iterables in Dart Programming
- Lists in Dart Programming
- Loops in Dart Programming
- Maps in Dart Programming
- Methods in Dart Programming
- Mixins in Dart Programming
- Queue in Dart Programming
- Runes in Dart Programming
- Set in Dart Programming
- String in Dart Programming
