flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
162.18k stars 26.64k forks source link

Proposal to make `LabeledGlobalKey` Constructor a const. #147508

Closed vishnuagbly closed 1 week ago

vishnuagbly commented 2 weeks ago

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

darshankawar commented 2 weeks 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 ?

vishnuagbly commented 2 weeks ago

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.

darshankawar commented 1 week ago

Thanks for the update. Keeping the issue open and labeling as proposal for team's input / tracking.

goderbauer commented 1 week ago

LabeledGlobalKey is non-const by design as documented on the constructor:

https://github.com/flutter/flutter/blob/54e66469a933b60ddf175f858f82eaeb97e48c8d/packages/flutter/lib/src/widgets/framework.dart#L204-L205

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.