I recently got into this problem.
Are the following Dart
statements equal or else what are the differences:
class StaticConstVsFinal {
static const timeout1 = const Duration(seconds: 5); // ------> 1
static const timeout2 = Duration(seconds: 5); // ------> 2
final timeout3 = const Duration(seconds: 5); // ------> 3
final timeout4 = Duration(seconds: 5); // ------> 4
}
As per my understanding static const
variables doesn't allocate memory no matter how many instances of StaticConstVsFinal
are made, memory usage won't increase as those are class variables, but for final
variables it increases memory usage. ---> Correct me if I'm wrong.
What are the pros and cons of defining these Flutter Widgets
as constants, is that essential too?
Please explain someone the differencs, pros and cons of each way if possible.
I know there's a much similar Question here but it doesn't cover all the above mentioned scenarios. So, please don't mark these as a duplicate.