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?

What is a const constructor?

  • Based on the above, a const constructor is:
    • A class constructor that only accepts compile-time constants as arguments
Reference Links

Discussion