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.
This commit is contained in:
Michael Goderbauer 2024-05-07 13:50:46 -07:00 committed by GitHub
parent 458c384fe2
commit 8796562d0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,7 +71,7 @@ class DialogDemoState extends State<DialogDemo> {
builder: (BuildContext context) => child!, builder: (BuildContext context) => child!,
) )
.then((T? value) { // The value passed to Navigator.pop() or null. .then((T? value) { // The value passed to Navigator.pop() or null.
if (value != null) { if (context.mounted && value != null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('You selected: $value'), content: Text('You selected: $value'),
)); ));
@ -179,6 +179,9 @@ class DialogDemoState extends State<DialogDemo> {
initialTime: _selectedTime!, initialTime: _selectedTime!,
) )
.then((TimeOfDay? value) { .then((TimeOfDay? value) {
if (!mounted) {
return;
}
if (value != null && value != _selectedTime) { if (value != null && value != _selectedTime) {
_selectedTime = value; _selectedTime = value;
ScaffoldMessenger.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(