Skip to content

Commit 399a649

Browse files
authored
Fix TooltipState null check error (#106330)
1 parent 61b3b39 commit 399a649

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/flutter/lib/src/material/tooltip.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ class TooltipState extends State<Tooltip> with SingleTickerProviderStateMixin {
529529
/// Returns `false` when the tooltip shouldn't be shown or when the tooltip
530530
/// was already visible.
531531
bool ensureTooltipVisible() {
532-
if (!_visible) {
532+
if (!_visible || !mounted) {
533533
return false;
534534
}
535535
_showTimer?.cancel();

packages/flutter/test/material/tooltip_test.dart

+31
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,37 @@ void main() {
10531053
gesture = null;
10541054
});
10551055

1056+
testWidgets('Calling ensureTooltipVisible on an unmounted TooltipState returns false', (WidgetTester tester) async {
1057+
// Regression test for https://github.com/flutter/flutter/issues/95851
1058+
await tester.pumpWidget(
1059+
const MaterialApp(
1060+
home: Center(
1061+
child: Tooltip(
1062+
message: tooltipText,
1063+
child: SizedBox(
1064+
width: 100.0,
1065+
height: 100.0,
1066+
),
1067+
),
1068+
),
1069+
),
1070+
);
1071+
1072+
final TooltipState tooltipState = tester.state(find.byType(Tooltip));
1073+
expect(tooltipState.ensureTooltipVisible(), true);
1074+
1075+
// Remove the tooltip.
1076+
await tester.pumpWidget(
1077+
const MaterialApp(
1078+
home: Center(
1079+
child: SizedBox.shrink(),
1080+
),
1081+
),
1082+
);
1083+
1084+
expect(tooltipState.ensureTooltipVisible(), false);
1085+
});
1086+
10561087
testWidgets('Tooltip shows/hides when hovered', (WidgetTester tester) async {
10571088
const Duration waitDuration = Duration.zero;
10581089
TestGesture? gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);

0 commit comments

Comments
 (0)