The constructor being called isn’t a const constructor.

When the constructor being called is not a const constructor, it means that the constructor does not have the ‘const’ keyword in its declaration. In Dart, the ‘const’ keyword is used to create a compile-time constant constructor.

A const constructor is a special type of constructor that can only be used to create objects whose fields are all compile-time constants. This means that the values assigned to the object’s fields must be known at compile-time.

Here’s an example to illustrate the difference between a normal constructor and a const constructor:

    
      class Person {
        final String name;
        final int age;
        
        // Normal constructor
        Person(this.name, this.age);
        
        // Const constructor
        const Person.constant(this.name, this.age);
      }
      
      void main() {
        // Using the normal constructor
        var person1 = Person('John', 25);
        
        // Using the const constructor
        var person2 = const Person.constant('Jane', 30);
      }
    
  

In the above example, the ‘Person’ class has two constructors – a normal constructor and a const constructor. The normal constructor is called without the ‘const’ keyword and allows non-constant values to be assigned to its fields. The const constructor is called with the ‘const’ keyword and requires all field values to be compile-time constants.

In the ‘main’ function, ‘person1’ is created using the normal constructor and ‘person2’ is created using the const constructor. The first object can have its fields assigned at runtime, while the second object’s fields must have constant values known at compile-time.

So, whenever a constructor is being called without the ‘const’ keyword, it means that it is not a const constructor. This allows for more flexibility in assigning values to the object’s fields, but it doesn’t enforce compile-time constantness.

Similar post

Leave a comment