sujay
Sat Oct 21 2023
Difference between const and final in Dart
• Even though both const
and final
cannot be reassigned, there is a subtle difference between them.
• const
variables are used for compile-time constants whereas final
variables are used for run-time constants.
const current_time = new DateTime.now() // DON'T do it as the value is computed at run time
const name = 'Rahul' // DO it as the value is known at compile time
• When reading from database or reading from a file, the values won't be known at compile time. Use final
in such cases