1

this might be a weak question to ask since it hasn't been long since I started using Dart.

If I got it right, a dart class constructor declared using the keyword "const" should be creating "compile time constant" objects, and those objects have only final fields and "const" declared objects, so every const object created will be using the same read-only resources defined in memory, and this would improve the performance of the app.

If I instantiate an object without the keyword "const", and using a const declared constructor, I will be getting a linting error saying that I should be prefixing the object instantiation with a "const" keyword.

So what's the point of prefixing the constructor call with "const" if it's already defined in the declaration with one? How come an instance not provided with "const" keyword not be "compile time constant" when the constructor is already declared to be "const"?

Ahmad Hamwi
  • 204
  • 2
  • 10
  • 1
    The point of *declaring* a constructor with `const` is to specify that the object *can* be constructed as a `const` object if all of its construction arguments also happen to be `const`. It does not mean that it *must* be constructed as a `const` objects. – jamesdlin Dec 04 '21 at 23:19

0 Answers0