Home > Software design >  Are all fields of the instance of the constant class still final or constant values if they are inst
Are all fields of the instance of the constant class still final or constant values if they are inst

Time:10-03

What happens if a constant constructor is outside of a constant context and is invoked without const, are all fields of the instance of the class still final or constant values? I know that it will be a non-constant object.

MaterialApp(); // Do NOT create a constant

CodePudding user response:

The const variables wrapped by the MaterialApp are still const.

It's a good practice to set a value as const when it is possible

CodePudding user response:

A class that has a const constructor must have only final (non-reassignable) fields, and that is part of the class interface (i.e., the class provides only getters for those fields and no setters).

Whether or not an instance of that class is a compile-time constant does not change that.

  •  Tags:  
  • dart
  • Related