Skip to content

Commit

Permalink
Don't use BuildContext across async gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Mazel committed Nov 5, 2023
1 parent 9a34eac commit 5f7bc96
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 35 deletions.
12 changes: 7 additions & 5 deletions lib/screens/auth_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,19 @@ class _AuthCardState extends State<AuthCard> {
_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;
});
}
}

Expand Down Expand Up @@ -249,7 +251,7 @@ class _AuthCardState extends State<AuthCard> {
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;
}

Expand Down
4 changes: 3 additions & 1 deletion lib/widgets/add_exercise/mixins/image_picker_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
24 changes: 18 additions & 6 deletions lib/widgets/measurements/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ class MeasurementCategoryForm extends StatelessWidget {
: await Provider.of<MeasurementProvider>(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();
},
),
],
Expand Down Expand Up @@ -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();
},
),
],
Expand Down
13 changes: 8 additions & 5 deletions lib/widgets/nutrition/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -392,17 +392,16 @@ class PlanForm extends StatelessWidget {

// Save to DB
try {
final navigator = Navigator.of(context);
if (_plan.id != null) {
await Provider.of<NutritionPlansProvider>(context, listen: false).editPlan(_plan);
if (context.mounted) {
navigator.pop();
Navigator.of(context).pop();
}
} else {
_plan = await Provider.of<NutritionPlansProvider>(context, listen: false)
.addPlan(_plan);
if (context.mounted) {
navigator.pushReplacementNamed(
Navigator.of(context).pushReplacementNamed(
NutritionalPlanScreen.routeName,
arguments: _plan,
);
Expand All @@ -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);
}
}
},
),
Expand Down
10 changes: 6 additions & 4 deletions lib/widgets/nutrition/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,12 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
}
}
} 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'),
Expand Down
12 changes: 9 additions & 3 deletions lib/widgets/weight/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,17 @@ class WeightForm extends StatelessWidget {
: await Provider.of<BodyWeightProvider>(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();
},
),
],
Expand Down
18 changes: 12 additions & 6 deletions lib/widgets/workouts/forms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,19 @@ class WorkoutForm extends StatelessWidget {
// Save to DB
if (_plan.id != null) {
await Provider.of<WorkoutPlansProvider>(context, listen: false).editWorkout(_plan);
Navigator.of(context).pop();
if (context.mounted) {
Navigator.of(context).pop();
}
} else {
final WorkoutPlan newPlan =
await Provider.of<WorkoutPlansProvider>(context, listen: false)
.addWorkout(_plan);
Navigator.of(context).pushReplacementNamed(
WorkoutPlanScreen.routeName,
arguments: newPlan,
);
if (context.mounted) {
Navigator.of(context).pushReplacementNamed(
WorkoutPlanScreen.routeName,
arguments: newPlan,
);
}
}
},
),
Expand Down Expand Up @@ -541,7 +545,9 @@ class _SetFormWidgetState extends State<SetFormWidget> {
widget._day.sets.add(widget._set);

// Close the bottom sheet
Navigator.of(context).pop();
if (context.mounted) {
Navigator.of(context).pop();
}
},
),
],
Expand Down
20 changes: 15 additions & 5 deletions lib/widgets/workouts/gym_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,14 @@ class _LogPageState extends State<LogPage> {
);
_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;
}
},
Expand Down Expand Up @@ -902,11 +906,17 @@ class _SessionPageState extends State<SessionPage> {
try {
await Provider.of<WorkoutPlansProvider>(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);
}
}
},
),
Expand Down

0 comments on commit 5f7bc96

Please sign in to comment.