FLUTTER INTERVIEW QUESTIONS

What’s the difference between const and final in Dart?

Jelena Lecic
1 min readDec 19, 2019

--

Both final and const prevent a variable from being reassigned and const variables are implicitly final.

So, once you assign value to a const or final variable, you can not change it.

A const variable is a compile-time constant, which means that it must be created from constants available in compile-time.

void main() {  const int a = 3;
final int b = 4;
const int c = a * 9;
const int d = a * b; //this will not compile
final DateTime now = DateTime.now();
const DateTime anotherNow = DateTime.now(); //this will not compile
a = 5; //this will not compile
b = 6; //this will not compile
}

📖 Use https://dartpad.dartlang.org/ for testing examples 📖

--

--