问题
I have Doubt when understand, Compile-time constant and runtime constant in Dart language, I am a beginner in dart language, I searched in google there is no article that covers this question, So thanks in advance
回答1:
There are no "run-time constants" in Dart, not the way the word "constant" is generally used. All constants are compile-time constants, meaning that their entire value can be determined at compile-time, they are deeply immutable, and the compiler can canonicalize the objects if two constant expressions end up with objects that have the exact same state.
The name "compile-time constants" wording comes from the specification which talks about "compile-time constant expressions". The results of those expressions are just called "constants".
You can say that final x = List<int>.unmodifiable([1]); defines a constant. It's certainly an object which cannot be modified, but it's not what would traditionally be called a constant in Dart terminology - it cannot be used in the places where the language requires a constant value.
来源:https://stackoverflow.com/questions/58859001/what-is-the-difference-between-compile-time-constant-and-run-time-constant