Skip to content

Remove unnecessary value key hack in key.dart #189291

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
AbdeMohlbi:abdemohlbi_remove_unecessary_value_key_hack
Jul 16, 2026
Merged

Remove unnecessary value key hack in key.dart #189291
auto-submit[bot] merged 3 commits into
flutter:masterfrom
AbdeMohlbi:abdemohlbi_remove_unecessary_value_key_hack

Conversation

@AbdeMohlbi

@AbdeMohlbi AbdeMohlbi commented Jul 10, 2026

Copy link
Copy Markdown
Member

while reading dart-lang/language#4725, i remembered #178219, the goal of #178219 was to remove the need for an additional class to get the type literal, but thanks to the unhealthy amount of reflection that i have been doing lately (i blame zig) i realized that we can now achieve this directly by checking for type equality, ie:

if (runtimeType == ValueKey<T>) {

for anyone wondering what is this about, here is a little example:

void main(List<String> arguments) {
  typeLitterals();
  anotherTypeLitteralsEaxmple();
}

void typeLitterals() {
  final int x = switch (int) {
    const (int) => 1,
    const (String) => 2,
    _ => 3,
  };
  print(x);
}

void anotherTypeLitteralsEaxmple() {
  const x = (int == int);
  print(x);
}

this prints

1
true

on the other hand :

@override
String toString() {
final valueString = T == String ? "<'$value'>" : '<$value>';
if (runtimeType == _TypeOf<ValueKey<T>>) {
return '[$valueString]';
}
return '[$T $valueString]';
}
}

the reason we have the

   // an instance of ValueKey<T> will hit this branch
if (runtimeType == ValueKey<T>) {
      return '[$valueString]';
    }
   // any classes that extend ValueKey and do not override `toString()` will hit this branch
    return '[$T $valueString]';

if all of this is still confusing check the following test (which passes on HEAD and this branch):

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  group('ValueKey toString behavior', () {
    test('contains only one value (Standard ValueKey)', () {
      const ValueKey<String> string = ValueKey('string');
      expect(string.toString(), "[<'string'>]");

      const ValueKey<int> c = ValueKey(2);
      expect(c.toString(), "[<2>]");
    });

    test('Subclass without override falls back to type-prefixed format', () {
      const badKey = BadCustomKey<String>('string');
      expect(badKey.toString(), "[String <'string'>]");
    });

    test('Subclass with override uses custom format', () {
      const goodKey = GoodCustomKey<String>('string');
      expect(goodKey.toString(), "[GoodCustomKey <string>]");
    });
  });
}

// missing toString implementation
class BadCustomKey<T> extends ValueKey<T> {
  const BadCustomKey(super.value);
}

class GoodCustomKey<T> extends ValueKey<T> {
  const GoodCustomKey(super.value);

  @override
  String toString() => '[GoodCustomKey <$value>]';
}

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Jul 10, 2026
@AbdeMohlbi AbdeMohlbi added the CICD Run CI/CD label Jul 10, 2026
@AbdeMohlbi AbdeMohlbi mentioned this pull request Jul 13, 2026
10 tasks
@AbdeMohlbi AbdeMohlbi changed the title remove uncessary value key hack Remove unnecessary value key hack in key.dart Jul 13, 2026
@AbdeMohlbi
AbdeMohlbi marked this pull request as ready for review July 13, 2026 21:08

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request simplifies the toString method in the ValueKey class by directly comparing runtimeType to ValueKey<T> and removing the unused _TypeOf typedef. There are no review comments, and I have no additional feedback to provide.

@AbdeMohlbi

Copy link
Copy Markdown
Member Author

Code Review

This pull request simplifies the toString method in the ValueKey class by directly comparing runtimeType to ValueKey<T> and removing the unused _TypeOf typedef. There are no review comments, and I have no additional feedback to provide.

jetstream

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 . Thanks for following up on this a year later!

@flutter-dashboard

Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@stuartmorgan-g

Copy link
Copy Markdown
Contributor

test-exempt: code refactor with no semantic change

@AbdeMohlbi AbdeMohlbi added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 16, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Jul 16, 2026
Merged via the queue into flutter:master with commit 36b35dd Jul 16, 2026
98 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jul 16, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Jul 18, 2026
Roll Flutter from fc1ad955f164 to 8005793c3562 (40 revisions)

flutter/flutter@fc1ad95...8005793

2026-07-18 [email protected] Remove `LineContents ` experimental AA line shader that is no longer used (flutter/flutter#189619)
2026-07-18 [email protected] Roll Skia from 9468e96cc40f to ba90f98535de (8 revisions) (flutter/flutter#189680)
2026-07-17 [email protected] Roll Dart SDK from 33e4b71e984b to 666e1e2133b7 (2 revisions) (flutter/flutter#189673)
2026-07-17 [email protected] [iOS] Fix potential use-after-free in a11y bridge channel handler (flutter/flutter#189637)
2026-07-17 [email protected] Migrate dev/tools skill validation to new dart_skills_lint API (flutter/flutter#189626)
2026-07-17 [email protected] Add validation for required fields during xcodebuild (flutter/flutter#187772)
2026-07-17 [email protected] Upgrade android_hardware_smoke_test CI to run instrumented tests (flutter/flutter#189390)
2026-07-17 [email protected] Roll Dart SDK from 0867cb1897b5 to 33e4b71e984b (1 revision) (flutter/flutter#189661)
2026-07-17 [email protected] Roll Skia from 702c3e790232 to 9468e96cc40f (1 revision) (flutter/flutter#189660)
2026-07-17 [email protected] Roll Packages from 9f95026 to 4fdc766 (11 revisions) (flutter/flutter#189659)
2026-07-17 [email protected] Roll Skia from 2e4a3ae035cd to 702c3e790232 (1 revision) (flutter/flutter#189655)
2026-07-17 [email protected] [macOS] Add FlutterPluginRegistrar.valuePublished(byPlugin:) just like iOS (flutter/flutter#189614)
2026-07-17 [email protected] Roll Dart SDK from c69d138c9646 to 0867cb1897b5 (2 revisions) (flutter/flutter#189649)
2026-07-17 [email protected] Roll Skia from 4f5aca109c87 to 2e4a3ae035cd (3 revisions) (flutter/flutter#189646)
2026-07-17 [email protected] [iOS] Fix missing nil checks and improve SemanticsObject bridge API (flutter/flutter#189630)
2026-07-17 [email protected] Roll Skia from 37c5e6b26aee to 4f5aca109c87 (19 revisions) (flutter/flutter#189638)
2026-07-17 [email protected] Roll Fuchsia Linux SDK from lLFbh5kFWbUGgC9Ek... to NL8xtzr8cxr5E8r8E... (flutter/flutter#189632)
2026-07-17 [email protected] Add blendMode parameter to RawImage and RenderImage (flutter/flutter#185938)
2026-07-17 [email protected] [web] Cache WASM network requests in flutter test (flutter/flutter#189623)
2026-07-17 [email protected] android_hardware_smoke_tests: Apply semantic line breaks to readme (flutter/flutter#189613)
2026-07-16 [email protected] ci(github-actions): resolve zizmor github-env findings in composite flutter actions (flutter/flutter#189602)
2026-07-16 [email protected] Remove obsolete packages analysis flag (flutter/flutter#189525)
2026-07-16 [email protected] Roll Dart SDK from e24870ff15bc to c69d138c9646 (2 revisions) (flutter/flutter#189597)
2026-07-16 [email protected] [fuchsia][iwyu] Remove transitive include of fuchsia.input.report. (flutter/flutter#188891)
2026-07-16 [email protected] Remove unnecessary value key hack in `key.dart`  (flutter/flutter#189291)
2026-07-16 [email protected] Roll Dart SDK from 81306a2ed317 to e24870ff15bc (1 revision) (flutter/flutter#189569)
2026-07-16 [email protected] [Impeller] Fix atlas growth test (flutter/flutter#189533)
2026-07-16 [email protected] Roll Dart SDK from d402ff7c9c84 to 81306a2ed317 (4 revisions) (flutter/flutter#189558)
2026-07-16 [email protected] Move shared darwin plugin tests into own builder (flutter/flutter#189411)
2026-07-16 [email protected] Roll Skia from ab2410bc857c to 37c5e6b26aee (19 revisions) (flutter/flutter#189549)
2026-07-16 [email protected] [ios] Fix //flutter:unittests build for physical devices (flutter/flutter#189543)
2026-07-16 [email protected] Fix lower DragTarget not being recognized in overlapping targets (flutter/flutter#188979)
2026-07-16 [email protected] Relocate cupertino samples that were under widgets/ (flutter/flutter#188876)
2026-07-15 [email protected] [iOS] Migrate DisplayLinkManager to a shared instance (flutter/flutter#189492)
2026-07-15 [email protected] [Impeller] Call glfwTerminate during global test environment teardown if a playground test called glfwInit (flutter/flutter#189523)
2026-07-15 [email protected] Roll pub packages (flutter/flutter#189515)
2026-07-15 [email protected] Roll Packages from ad2eab1 to 9f95026 (8 revisions) (flutter/flutter#189509)
2026-07-15 [email protected] add `@nonVirtual` to `RenderObject.attached`, fix `WidgetTester.hasRunningAnimations` (flutter/flutter#186832)
2026-07-15 [email protected] Roll vulkan-deps to 0582f446e54a (flutter/flutter#188524)
2026-07-15 [email protected] Roll Dart SDK from 0c408ff6dce9 to d402ff7c9c84 (1 revision) (flutter/flutter#189497)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants