Skip to content

Instantly share code, notes, and snippets.

@frank-weindel
Last active March 21, 2025 13:28
Show Gist options
  • Save frank-weindel/9831b2f4baa79f9e067dfbf70009f88b to your computer and use it in GitHub Desktop.
Save frank-weindel/9831b2f4baa79f9e067dfbf70009f88b to your computer and use it in GitHub Desktop.
Dart Extension Types as Map Keys (Be Careful)
extension type ChannelId(String value) {}
extension type StationId(String value) {}
extension type SomeNumberId(int value) {}
Map<ChannelId, String> tvChannelNames = {ChannelId('123'): 'NBC'};
void main() {
print(tvChannelNames[StationId('123')]); // No Error!
print(tvChannelNames[SomeNumberId(123)]); // No Error!
print(tvChannelNames[123]); // collection_methods_unrelated_type lint rule
print(tvChannelNames["123"]); // collection_methods_unrelated_type lint rule
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment