iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙌
What are const Constructors in Dart?
First, you need to understand the terms final, const, and compile-time constant.
- final
- Initialized only once after initialization starts
- Reassignment is not allowed
- The contents of the allocated memory can be modified
-
final hoge = [1,2,3] hoge = [] // Error hoge[0] = 10 // No error
-
- const
- A constant determined at compile time = compile-time constant
- Reassignment is not allowed
- The contents of the allocated memory cannot be modified either
const hoge = [1,2,3] hoge = [] // Error hoge[0] = 10 // Error
- What is a compile-time constant?
-
Including variables specified as const, the more general concept is the "compile-time constant".
- Literals such as
1,"abc", or[1,2,3]. In short, values that are not determined dynamically.
-
What is a const constructor?
- Based on the above, a const constructor is:
- A class constructor that only accepts compile-time constants as arguments
Discussion