Closed vishnuagbly closed 1 week ago
@vishnuagbly Can you elaborate on the use case which requires you to make the class const and how does it will help to resolve your case ?
Like, I am trying to make a class, that will hold a GlobalKey, and I cannot make that class with the constant constructor, because GlobalKeys cannot be constant. And I think, we can make this constant, as it does not cause any other errors as well. We just need to add a const
keyword there.
Thanks for the update. Keeping the issue open and labeling as proposal for team's input / tracking.
LabeledGlobalKey is non-const by design as documented on the constructor:
If we were to make this const all LabeledGlobalKey with the same label would actually have the same identity which will have surprising side effects. Instead, each instantiation of LabledGlobalKey must have its own identity and therefore it cannot be const.
If you're looking for a GlobalKey that can be const you can use GlobalObjectKey instead, which derives its identity from the provided object instead.
We just need to add "const" on the below line.
https://github.com/flutter/flutter/blob/54e66469a933b60ddf175f858f82eaeb97e48c8d/packages/flutter/lib/src/widgets/framework.dart#L205