Skip to content

Commit 1b2ee41

Browse files
Sort diagnostics node dependencies so that order is stable (#105319)
* Sort diagnostics node dependencies so that order is stable * use toDescription * use toStringShort when possible * sort InheritedElements
1 parent 95eb7a3 commit 1b2ee41

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

packages/flutter/lib/src/widgets/framework.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -4452,8 +4452,12 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
44524452
properties.add(DiagnosticsProperty<Key>('key', _widget?.key, showName: false, defaultValue: null, level: DiagnosticLevel.hidden));
44534453
_widget?.debugFillProperties(properties);
44544454
properties.add(FlagProperty('dirty', value: dirty, ifTrue: 'dirty'));
4455-
if (_dependencies != null && _dependencies!.isNotEmpty) {
4456-
final List<DiagnosticsNode> diagnosticsDependencies = _dependencies!
4455+
final Set<InheritedElement>? deps = _dependencies;
4456+
if (deps != null && deps.isNotEmpty) {
4457+
final List<InheritedElement> sortedDependencies = deps.toList()
4458+
..sort((InheritedElement a, InheritedElement b) =>
4459+
a.toStringShort().compareTo(b.toStringShort()));
4460+
final List<DiagnosticsNode> diagnosticsDependencies = sortedDependencies
44574461
.map((InheritedElement element) => element.widget.toDiagnosticsNode(style: DiagnosticsTreeStyle.sparse))
44584462
.toList();
44594463
properties.add(DiagnosticsProperty<List<DiagnosticsNode>>('dependencies', diagnosticsDependencies));

packages/flutter/test/widgets/framework_test.dart

+44
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,50 @@ void main() {
15581558
expect(builder.properties.any((DiagnosticsNode property) => property.name == 'renderObject' && property.value == null), isTrue);
15591559
});
15601560

1561+
testWidgets('debugFillProperties sorts dependencies in alphabetical order', (WidgetTester tester) async {
1562+
final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
1563+
final TestRenderObjectElement element = TestRenderObjectElement();
1564+
1565+
final _TestInheritedElement focusTraversalOrder =
1566+
_TestInheritedElement(const FocusTraversalOrder(
1567+
order: LexicalFocusOrder(''),
1568+
child: Placeholder(),
1569+
));
1570+
final _TestInheritedElement directionality =
1571+
_TestInheritedElement(const Directionality(
1572+
textDirection: TextDirection.ltr,
1573+
child: Placeholder(),
1574+
));
1575+
final _TestInheritedElement buttonBarTheme =
1576+
_TestInheritedElement(const ButtonBarTheme(
1577+
data: ButtonBarThemeData(
1578+
alignment: MainAxisAlignment.center,
1579+
),
1580+
child: Placeholder(),
1581+
));
1582+
1583+
// Dependencies are added out of alphabetical order.
1584+
element
1585+
..dependOnInheritedElement(focusTraversalOrder)
1586+
..dependOnInheritedElement(directionality)
1587+
..dependOnInheritedElement(buttonBarTheme);
1588+
1589+
// Dependencies will be sorted by [debugFillProperties].
1590+
element.debugFillProperties(builder);
1591+
1592+
expect(
1593+
builder.properties.any((DiagnosticsNode property) => property.name == 'dependencies' && property.value != null),
1594+
isTrue,
1595+
);
1596+
final DiagnosticsProperty<List<DiagnosticsNode>> dependenciesProperty =
1597+
builder.properties.firstWhere((DiagnosticsNode property) => property.name == 'dependencies') as DiagnosticsProperty<List<DiagnosticsNode>>;
1598+
expect(dependenciesProperty, isNotNull);
1599+
1600+
final List<DiagnosticsNode> dependencies = dependenciesProperty.value!;
1601+
expect(dependencies.length, equals(3));
1602+
expect(dependencies.toString(), '[ButtonBarTheme, Directionality, FocusTraversalOrder]');
1603+
});
1604+
15611605
testWidgets('BuildOwner.globalKeyCount keeps track of in-use global keys', (WidgetTester tester) async {
15621606
final int initialCount = tester.binding.buildOwner!.globalKeyCount;
15631607
final GlobalKey key1 = GlobalKey();

0 commit comments

Comments
 (0)