From 8796562d0ac9c75d2a4341fd06eee8ddc77987fc Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Tue, 7 May 2024 13:50:46 -0700 Subject: [PATCH] Properly guard context access in then clauses (#147935) Prepares for a fix to the `use_build_context_synchronously` lint (https://dart-review.googlesource.com/c/sdk/+/365541) that will complain about these unsafe usages. --- .../flutter_gallery/lib/demo/material/dialog_demo.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/dev/integration_tests/flutter_gallery/lib/demo/material/dialog_demo.dart b/dev/integration_tests/flutter_gallery/lib/demo/material/dialog_demo.dart index 04a2c13b5f..764670166c 100644 --- a/dev/integration_tests/flutter_gallery/lib/demo/material/dialog_demo.dart +++ b/dev/integration_tests/flutter_gallery/lib/demo/material/dialog_demo.dart @@ -71,7 +71,7 @@ class DialogDemoState extends State { builder: (BuildContext context) => child!, ) .then((T? value) { // The value passed to Navigator.pop() or null. - if (value != null) { + if (context.mounted && value != null) { ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text('You selected: $value'), )); @@ -179,6 +179,9 @@ class DialogDemoState extends State { initialTime: _selectedTime!, ) .then((TimeOfDay? value) { + if (!mounted) { + return; + } if (value != null && value != _selectedTime) { _selectedTime = value; ScaffoldMessenger.of(context).showSnackBar(SnackBar(