I have this piece of code that intend to use in a stateless widget in flutter.
const String default_question = 'the question';
class Trial {
final String question;
const Trial.standard() :
question = default_question;
}
I use it like this:
final Trial _trial = const Trial.standard();
And this works. But it doesn't work without using const. And I want to understand why const is neccessary here. Because I plan on using a constructor that is not constant in the future.
CodePudding user response:
cosnt marked widgets or variables are built only once in flutter framework which helps in performance improvement.
And this is done by a packages flutter_lints which is added in pubspec.yaml by default in latest flutter versions. You can check docs at given at flutter official website.
If you don't want to use this thing, simply remove the package from pubspec.yaml
Cheers!
CodePudding user response:
Well, hamza is right, it is better that you adapt to the new improvements so that you have better productivity using the widgets.
