Skip to content

Commit 459df3c

Browse files
Fix memory leaks in CupertinoSwitch (#147821)
1 parent 02d239a commit 459df3c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

packages/flutter/lib/src/cupertino/switch.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
200200
late HorizontalDragGestureRecognizer _drag;
201201

202202
late AnimationController _positionController;
203-
late CurvedAnimation position;
203+
late final CurvedAnimation position;
204204

205205
late AnimationController _reactionController;
206-
late Animation<double> _reaction;
206+
late CurvedAnimation _reaction;
207207

208208
late bool isFocused;
209209

@@ -424,6 +424,8 @@ class _CupertinoSwitchState extends State<CupertinoSwitch> with TickerProviderSt
424424

425425
_positionController.dispose();
426426
_reactionController.dispose();
427+
position.dispose();
428+
_reaction.dispose();
427429
super.dispose();
428430
}
429431
}

packages/flutter/test/cupertino/switch_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ import 'package:flutter/material.dart';
1414
import 'package:flutter/rendering.dart';
1515
import 'package:flutter/services.dart';
1616
import 'package:flutter_test/flutter_test.dart';
17+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1718

1819
void main() {
19-
testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
20+
testWidgets('Switch can toggle on tap',
21+
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
22+
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const <String>['CurvedAnimation']),
23+
(WidgetTester tester) async {
2024
final Key switchKey = UniqueKey();
2125
bool value = false;
2226
await tester.pumpWidget(

0 commit comments

Comments
 (0)