Change GlobalObjectKey.toString to strip away State<StatefulWidget>. (#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<State<StatefulWidget>> 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).
This commit is contained in:
parent
fa122f5a6b
commit
d20125c3da
@ -312,7 +312,17 @@ class GlobalObjectKey<T extends State<StatefulWidget>> extends GlobalKey<T> {
|
|||||||
int get hashCode => identityHashCode(value);
|
int get hashCode => identityHashCode(value);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() => '[$runtimeType ${describeIdentity(value)}]';
|
String toString() {
|
||||||
|
String selfType = runtimeType.toString();
|
||||||
|
// const GlobalObjectKey().runtimeType.toString() returns 'GlobalObjectKey<State<StatefulWidget>>'
|
||||||
|
// because GlobalObjectKey is instantiated to its bounds. To avoid cluttering the output
|
||||||
|
// we remove the suffix.
|
||||||
|
const String suffix = '<State<StatefulWidget>>';
|
||||||
|
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
|
/// This class is a work-around for the "is" operator not accepting a variable value as its right operand
|
||||||
|
@ -11,6 +11,11 @@ class TestState extends State<StatefulWidget> {
|
|||||||
Widget build(BuildContext context) => null;
|
Widget build(BuildContext context) => null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@optionalTypeArgs
|
||||||
|
class _MyGlobalObjectKey<T extends State<StatefulWidget>> extends GlobalObjectKey<T> {
|
||||||
|
const _MyGlobalObjectKey(Object value) : super(value);
|
||||||
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
testWidgets('UniqueKey control test', (WidgetTester tester) async {
|
testWidgets('UniqueKey control test', (WidgetTester tester) async {
|
||||||
final Key key = new UniqueKey();
|
final Key key = new UniqueKey();
|
||||||
@ -31,6 +36,18 @@ void main() {
|
|||||||
expect(keyA, isNot(equals(keyB)));
|
expect(keyA, isNot(equals(keyB)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('GlobalObjectKey toString test', (WidgetTester tester) async {
|
||||||
|
final GlobalObjectKey one = const GlobalObjectKey(1);
|
||||||
|
final GlobalObjectKey<TestState> two = const GlobalObjectKey<TestState>(2);
|
||||||
|
final GlobalObjectKey three = const _MyGlobalObjectKey(3);
|
||||||
|
final GlobalObjectKey<TestState> four = const _MyGlobalObjectKey<TestState>(4);
|
||||||
|
|
||||||
|
expect(one.toString(), equals('[GlobalObjectKey ${describeIdentity(1)}]'));
|
||||||
|
expect(two.toString(), equals('[GlobalObjectKey<TestState> ${describeIdentity(2)}]'));
|
||||||
|
expect(three.toString(), equals('[_MyGlobalObjectKey ${describeIdentity(3)}]'));
|
||||||
|
expect(four.toString(), equals('[_MyGlobalObjectKey<TestState> ${describeIdentity(4)}]'));
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('GlobalObjectKey control test', (WidgetTester tester) async {
|
testWidgets('GlobalObjectKey control test', (WidgetTester tester) async {
|
||||||
final Object a = new Object();
|
final Object a = new Object();
|
||||||
final Object b = new Object();
|
final Object b = new Object();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user