diff --git a/lib/screens/auth_screen.dart b/lib/screens/auth_screen.dart index 98f08072d..7424ae8f7 100644 --- a/lib/screens/auth_screen.dart +++ b/lib/screens/auth_screen.dart @@ -187,17 +187,19 @@ class _AuthCardState extends State { _isLoading = false; }); } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (mounted) { + showHttpExceptionErrorDialog(error, context); + } setState(() { _isLoading = false; }); } catch (error) { if (mounted) { showErrorDialog(error, context); - setState(() { - _isLoading = false; - }); } + setState(() { + _isLoading = false; + }); } } @@ -249,7 +251,7 @@ class _AuthCardState extends State { if (value == null || value.isEmpty) { return AppLocalizations.of(context).invalidUsername; } - if (!RegExp(r'^[\w.@+-]+$').hasMatch(value!)) { + if (!RegExp(r'^[\w.@+-]+$').hasMatch(value)) { return AppLocalizations.of(context).usernameValidChars; } diff --git a/lib/widgets/add_exercise/mixins/image_picker_mixin.dart b/lib/widgets/add_exercise/mixins/image_picker_mixin.dart index 425b0308e..d98a235ee 100644 --- a/lib/widgets/add_exercise/mixins/image_picker_mixin.dart +++ b/lib/widgets/add_exercise/mixins/image_picker_mixin.dart @@ -49,7 +49,9 @@ mixin ExerciseImagePickerMixin { } if (!isFileValid) { - showDialog(context: context, builder: (context) => Text(errorMessage)); + if (context.mounted) { + showDialog(context: context, builder: (context) => Text(errorMessage)); + } return; } } diff --git a/lib/widgets/measurements/forms.dart b/lib/widgets/measurements/forms.dart index ab3f91989..4e6e09e9a 100644 --- a/lib/widgets/measurements/forms.dart +++ b/lib/widgets/measurements/forms.dart @@ -109,11 +109,17 @@ class MeasurementCategoryForm extends StatelessWidget { : await Provider.of(context, listen: false).editCategory( categoryData['id'], categoryData['name'], categoryData['unit']); } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (context.mounted) { + showHttpExceptionErrorDialog(error, context); + } } catch (error) { - showErrorDialog(error, context); + if (context.mounted) { + showErrorDialog(error, context); + } + } + if (context.mounted) { + Navigator.of(context).pop(); } - Navigator.of(context).pop(); }, ), ], @@ -271,11 +277,17 @@ class MeasurementEntryForm extends StatelessWidget { _entryData['date'], ); } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (context.mounted) { + showHttpExceptionErrorDialog(error, context); + } } catch (error) { - showErrorDialog(error, context); + if (context.mounted) { + showErrorDialog(error, context); + } + } + if (context.mounted) { + Navigator.of(context).pop(); } - Navigator.of(context).pop(); }, ), ], diff --git a/lib/widgets/nutrition/forms.dart b/lib/widgets/nutrition/forms.dart index a634aea72..b0b002253 100644 --- a/lib/widgets/nutrition/forms.dart +++ b/lib/widgets/nutrition/forms.dart @@ -392,17 +392,16 @@ class PlanForm extends StatelessWidget { // Save to DB try { - final navigator = Navigator.of(context); if (_plan.id != null) { await Provider.of(context, listen: false).editPlan(_plan); if (context.mounted) { - navigator.pop(); + Navigator.of(context).pop(); } } else { _plan = await Provider.of(context, listen: false) .addPlan(_plan); if (context.mounted) { - navigator.pushReplacementNamed( + Navigator.of(context).pushReplacementNamed( NutritionalPlanScreen.routeName, arguments: _plan, ); @@ -412,9 +411,13 @@ class PlanForm extends StatelessWidget { // Saving was successful, reset the data _descriptionController.clear(); } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (context.mounted) { + showHttpExceptionErrorDialog(error, context); + } } catch (error) { - showErrorDialog(error, context); + if (context.mounted) { + showErrorDialog(error, context); + } } }, ), diff --git a/lib/widgets/nutrition/widgets.dart b/lib/widgets/nutrition/widgets.dart index d9dfc8bb4..977eae0aa 100644 --- a/lib/widgets/nutrition/widgets.dart +++ b/lib/widgets/nutrition/widgets.dart @@ -207,10 +207,12 @@ class _IngredientTypeaheadState extends State { } } } catch (e) { - showErrorDialog(e, context); - // Need to pop back since reader scan is a widget - // otherwise returns null when back button is pressed - return Navigator.pop(context); + if (context.mounted) { + showErrorDialog(e, context); + // Need to pop back since reader scan is a widget + // otherwise returns null when back button is pressed + return Navigator.pop(context); + } } }, icon: Image.asset('assets/images/barcode_scanner_icon.png'), diff --git a/lib/widgets/weight/forms.dart b/lib/widgets/weight/forms.dart index edf773525..c4b6f6760 100644 --- a/lib/widgets/weight/forms.dart +++ b/lib/widgets/weight/forms.dart @@ -119,11 +119,17 @@ class WeightForm extends StatelessWidget { : await Provider.of(context, listen: false) .editEntry(_weightEntry); } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (context.mounted) { + showHttpExceptionErrorDialog(error, context); + } } catch (error) { - showErrorDialog(error, context); + if (context.mounted) { + showErrorDialog(error, context); + } + } + if (context.mounted) { + Navigator.of(context).pop(); } - Navigator.of(context).pop(); }, ), ], diff --git a/lib/widgets/workouts/forms.dart b/lib/widgets/workouts/forms.dart index 0a13b1cef..3de5b3a66 100644 --- a/lib/widgets/workouts/forms.dart +++ b/lib/widgets/workouts/forms.dart @@ -104,15 +104,19 @@ class WorkoutForm extends StatelessWidget { // Save to DB if (_plan.id != null) { await Provider.of(context, listen: false).editWorkout(_plan); - Navigator.of(context).pop(); + if (context.mounted) { + Navigator.of(context).pop(); + } } else { final WorkoutPlan newPlan = await Provider.of(context, listen: false) .addWorkout(_plan); - Navigator.of(context).pushReplacementNamed( - WorkoutPlanScreen.routeName, - arguments: newPlan, - ); + if (context.mounted) { + Navigator.of(context).pushReplacementNamed( + WorkoutPlanScreen.routeName, + arguments: newPlan, + ); + } } }, ), @@ -541,7 +545,9 @@ class _SetFormWidgetState extends State { widget._day.sets.add(widget._set); // Close the bottom sheet - Navigator.of(context).pop(); + if (context.mounted) { + Navigator.of(context).pop(); + } }, ), ], diff --git a/lib/widgets/workouts/gym_mode.dart b/lib/widgets/workouts/gym_mode.dart index b9a787217..c6731a461 100644 --- a/lib/widgets/workouts/gym_mode.dart +++ b/lib/widgets/workouts/gym_mode.dart @@ -496,10 +496,14 @@ class _LogPageState extends State { ); _isSaving = false; } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (mounted) { + showHttpExceptionErrorDialog(error, context); + } _isSaving = false; } catch (error) { - showErrorDialog(error, context); + if (mounted) { + showErrorDialog(error, context); + } _isSaving = false; } }, @@ -902,11 +906,17 @@ class _SessionPageState extends State { try { await Provider.of(context, listen: false) .addSession(_session); - Navigator.of(context).pop(); + if (mounted) { + Navigator.of(context).pop(); + } } on WgerHttpException catch (error) { - showHttpExceptionErrorDialog(error, context); + if (mounted) { + showHttpExceptionErrorDialog(error, context); + } } catch (error) { - showErrorDialog(error, context); + if (mounted) { + showErrorDialog(error, context); + } } }, ),