Add assert for index parameter in IndexedStack. (#167757)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

Closes https://github.com/flutter/flutter/issues/165840 by adding an
assert with a more user-friendly message to the IndexedStack.

## Pre-launch Checklist

- [X] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [X] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [X] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [X] I signed the [CLA].
- [X] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [X] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [X] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [X] All existing and new tests are passing.

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

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Kostia Sokolovskyi 2025-05-13 17:51:20 +02:00 committed by GitHub
parent 358b072688
commit 336a7ecf7b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 1 deletions

View File

@ -4265,7 +4265,13 @@ class _RawIndexedStack extends Stack {
StackFit sizing = StackFit.loose,
this.index = 0,
super.children,
}) : super(fit: sizing);
}) : assert(
index == null ||
(index == 0 && children.length == 0) ||
(index >= 0 && index < children.length),
'The index must be null or within the range of children.',
),
super(fit: sizing);
/// The index of the child to show.
final int? index;

View File

@ -1001,6 +1001,51 @@ void main() {
'BoxConstraints(2.0<=w<=3.0, 5.0<=h<=7.0)',
]);
});
testWidgets('IndexedStack does not assert with the default parameters', (
WidgetTester tester,
) async {
await tester.pumpWidget(
const Directionality(textDirection: TextDirection.ltr, child: IndexedStack()),
);
expect(tester.takeException(), isNull);
});
testWidgets('IndexedStack does not assert when index is null', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IndexedStack(index: null, children: <Widget>[SizedBox.shrink(), SizedBox.shrink()]),
),
);
expect(tester.takeException(), isNull);
});
testWidgets('IndexedStack asserts when index is negative', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IndexedStack(index: -1, children: <Widget>[SizedBox.shrink(), SizedBox.shrink()]),
),
);
expect(tester.takeException(), isA<AssertionError>());
});
testWidgets('IndexedStack asserts when index is not in children range', (
WidgetTester tester,
) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IndexedStack(index: 2, children: <Widget>[SizedBox.shrink(), SizedBox.shrink()]),
),
);
expect(tester.takeException(), isA<AssertionError>());
});
}
class _ShowVisibility extends StatelessWidget {