Skip to content

Commit

Permalink
Properly guard context access in then clauses (#147935)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
goderbauer authored May 7, 2024
1 parent 458c384 commit 8796562
Showing 1 changed file with 4 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class DialogDemoState extends State<DialogDemo> {
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'),
));
Expand Down Expand Up @@ -179,6 +179,9 @@ class DialogDemoState extends State<DialogDemo> {
initialTime: _selectedTime!,
)
.then((TimeOfDay? value) {
if (!mounted) {
return;
}
if (value != null && value != _selectedTime) {
_selectedTime = value;
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Expand Down

0 comments on commit 8796562

Please sign in to comment.