From d20125c3da84d7caf4c0cb8e2fc5c2cc58d1d9df Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Fri, 9 Feb 2018 09:51:58 +0100 Subject: [PATCH] Change GlobalObjectKey.toString to strip away State. (#14558) This allows const GlobalObjectKey(0) to be concisely formatted as [GlobalObjectKey int#0000] in both Dart 2 and Dart 1 modes. Without this change it would be formatted as [GlobalObjectKey> int#0000] because in Dart 2 types are instantiated to bounds. In addition to retaining general readability this also fixes few tests that rely on this short string representation (see test/widgets/global_keys_duplicated_test.dart). --- .../flutter/lib/src/widgets/framework.dart | 12 +++++++++++- .../flutter/test/widgets/framework_test.dart | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index af63ffa0f3b..a1a81b6eacf 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -312,7 +312,17 @@ class GlobalObjectKey> extends GlobalKey { int get hashCode => identityHashCode(value); @override - String toString() => '[$runtimeType ${describeIdentity(value)}]'; + String toString() { + String selfType = runtimeType.toString(); + // const GlobalObjectKey().runtimeType.toString() returns 'GlobalObjectKey>' + // because GlobalObjectKey is instantiated to its bounds. To avoid cluttering the output + // we remove the suffix. + const String suffix = '>'; + if (selfType.endsWith(suffix)) { + selfType = selfType.substring(0, selfType.length - suffix.length); + } + return '[$selfType ${describeIdentity(value)}]'; + } } /// This class is a work-around for the "is" operator not accepting a variable value as its right operand diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index e464d2f5027..63ac6dc47b1 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -11,6 +11,11 @@ class TestState extends State { Widget build(BuildContext context) => null; } +@optionalTypeArgs +class _MyGlobalObjectKey> extends GlobalObjectKey { + const _MyGlobalObjectKey(Object value) : super(value); +} + void main() { testWidgets('UniqueKey control test', (WidgetTester tester) async { final Key key = new UniqueKey(); @@ -31,6 +36,18 @@ void main() { expect(keyA, isNot(equals(keyB))); }); + testWidgets('GlobalObjectKey toString test', (WidgetTester tester) async { + final GlobalObjectKey one = const GlobalObjectKey(1); + final GlobalObjectKey two = const GlobalObjectKey(2); + final GlobalObjectKey three = const _MyGlobalObjectKey(3); + final GlobalObjectKey four = const _MyGlobalObjectKey(4); + + expect(one.toString(), equals('[GlobalObjectKey ${describeIdentity(1)}]')); + expect(two.toString(), equals('[GlobalObjectKey ${describeIdentity(2)}]')); + expect(three.toString(), equals('[_MyGlobalObjectKey ${describeIdentity(3)}]')); + expect(four.toString(), equals('[_MyGlobalObjectKey ${describeIdentity(4)}]')); + }); + testWidgets('GlobalObjectKey control test', (WidgetTester tester) async { final Object a = new Object(); final Object b = new Object(); @@ -575,4 +592,4 @@ class NullChildElement extends Element { @override void performRebuild() { } -} \ No newline at end of file +}