Skip to content

Commit

Permalink
fix: 1815 - around context.mounted (#4768)
Browse files Browse the repository at this point in the history
* fix: 1815 - around context.mounted

The PR is about two related topics:
* Removing all the `ignore: use_build_context_synchronously`
* Replacing all the `State<StatefulWidget> widget` by `BuildContext context`, now that it's possible in flutter

* Update packages/smooth_app/lib/pages/onboarding/permissions_page.dart

* format fix
  • Loading branch information
monsieurtanuki authored Nov 15, 2023
1 parent 1024559 commit 346c6e3
Show file tree
Hide file tree
Showing 44 changed files with 271 additions and 177 deletions.
31 changes: 15 additions & 16 deletions packages/smooth_app/lib/background/background_task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,31 +111,30 @@ abstract class BackgroundTask {
@protected
Future<void> addToManager(
final LocalDatabase localDatabase, {
final State<StatefulWidget>? widget,
final BuildContext? context,
final bool showSnackBar = true,
}) async {
await BackgroundTaskManager.getInstance(localDatabase).add(this);
if (widget == null || !widget.mounted) {
if (context == null || !context.mounted) {
return;
}
if (!showSnackBar) {
return;
}

if (widget.context.mounted) {
// ignore: use_build_context_synchronously
if (getFloatingMessage(AppLocalizations.of(widget.context))
case (
final String message,
final AlignmentGeometry alignment,
)) {
// ignore: use_build_context_synchronously
SmoothFloatingMessage(message: message).show(
widget.context,
duration: SnackBarDuration.medium,
alignment: alignment,
);
}
if (!context.mounted) {
return;
}
if (getFloatingMessage(AppLocalizations.of(context))
case (
final String message,
final AlignmentGeometry alignment,
)) {
SmoothFloatingMessage(message: message).show(
context,
duration: SnackBarDuration.medium,
alignment: alignment,
);
}
}

Expand Down
9 changes: 6 additions & 3 deletions packages/smooth_app/lib/background/background_task_crop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
required final int y1,
required final int x2,
required final int y2,
required final State<StatefulWidget> widget,
required final BuildContext context,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: barcode,
Expand All @@ -81,7 +81,10 @@ class BackgroundTaskCrop extends BackgroundTaskUpload {
x2,
y2,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
/// Adds the background task about changing a product.
static Future<void> addTask(
final Product minimalistProduct, {
required final State<StatefulWidget> widget,
required final BuildContext context,
required final BackgroundTaskDetailsStamp stamp,
final bool showSnackBar = true,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: minimalistProduct.barcode,
Expand All @@ -83,9 +83,12 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode {
uniqueId,
stamp,
);
if (!context.mounted) {
return;
}
await task.addToManager(
localDatabase,
widget: widget,
context: context,
showSnackBar: showSnackBar,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ class BackgroundTaskFullRefresh extends BackgroundTaskPaged {
static const OperationType _operationType = OperationType.fullRefresh;

static Future<void> addTask({
required final State<StatefulWidget> widget,
required final BuildContext context,
required final int pageSize,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
);
final BackgroundTask task = _getNewTask(
uniqueId,
pageSize,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
required final String barcode,
required final String insightId,
required final InsightAnnotation insightAnnotation,
required final State<StatefulWidget> widget,
required final BuildContext context,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: barcode,
Expand All @@ -63,7 +63,10 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode {
insightAnnotation.value,
uniqueId,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
9 changes: 6 additions & 3 deletions packages/smooth_app/lib/background/background_task_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
required final int y1,
required final int x2,
required final int y2,
required final State<StatefulWidget> widget,
required final BuildContext context,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: barcode,
Expand All @@ -91,7 +91,10 @@ class BackgroundTaskImage extends BackgroundTaskUpload {
x2,
y2,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ class BackgroundTaskOffline extends BackgroundTaskProgressing {
static const OperationType _operationType = OperationType.offline;

static Future<void> addTask({
required final State<StatefulWidget> widget,
required final BuildContext context,
required final int pageSize,
required final int totalSize,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
totalSize: totalSize,
Expand All @@ -47,7 +47,10 @@ class BackgroundTaskOffline extends BackgroundTaskProgressing {
pageSize,
totalSize,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode {
static Future<void> addTask(
final String barcode, {
required final ImageField imageField,
required final State<StatefulWidget> widget,
required final BuildContext context,
required final OpenFoodFactsLanguage language,
}) async {
final LocalDatabase localDatabase = widget.context.read<LocalDatabase>();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final String uniqueId = await _operationType.getNewKey(
localDatabase,
barcode: barcode,
Expand All @@ -61,7 +61,10 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode {
uniqueId,
language,
);
await task.addToManager(localDatabase, widget: widget);
if (!context.mounted) {
return;
}
await task.addToManager(localDatabase, context: context);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _ProductImageCarouselItemState extends State<ProductImageCarouselItem> {
if (imageProvider == null) {
return ElevatedButton.icon(
onPressed: () async => confirmAndUploadNewPicture(
this,
context,
barcode: widget.product.barcode!,
imageField: widget.productImageData.imageField,
language: ProductQuery.getLanguage(),
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/data_models/onboarding_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ class OnboardingLoader {
dismissible: false,
);
if (downloaded != true) {
//ignore: use_build_context_synchronously
if (!context.mounted) {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(appLocalizations.onboarding_welcome_loading_error),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class _KnowledgePanelPageState extends State<KnowledgePanelPage>
}
await ProductRefresher().fetchAndRefresh(
barcode: barcode ?? '',
widget: this,
context: context,
);
} catch (e) {
//no refreshing during onboarding
Expand Down
56 changes: 30 additions & 26 deletions packages/smooth_app/lib/pages/crop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -339,38 +339,42 @@ class _CropPageState extends State<CropPage> {
sequenceNumber,
);
final Rect cropRect = _getLocalCropRect();
await BackgroundTaskImage.addTask(
widget.barcode,
language: widget.language,
imageField: widget.imageField,
fullFile: fullFile,
croppedFile: croppedFile,
rotation: _controller.rotation.degrees,
x1: cropRect.left.ceil(),
y1: cropRect.top.ceil(),
x2: cropRect.right.floor(),
y2: cropRect.bottom.floor(),
widget: this,
);
if (context.mounted) {
await BackgroundTaskImage.addTask(
widget.barcode,
language: widget.language,
imageField: widget.imageField,
fullFile: fullFile,
croppedFile: croppedFile,
rotation: _controller.rotation.degrees,
x1: cropRect.left.ceil(),
y1: cropRect.top.ceil(),
x2: cropRect.right.floor(),
y2: cropRect.bottom.floor(),
context: context,
);
}
} else {
// in this case, it's an existing picture, with crop parameters.
// we let the server do everything: better performance, and no privacy
// issue here (we're cropping from an allegedly already privacy compliant
// picture).
final Rect cropRect = _getServerCropRect();
await BackgroundTaskCrop.addTask(
widget.barcode,
language: widget.language,
imageField: widget.imageField,
imageId: widget.imageId!,
croppedFile: croppedFile,
rotation: _controller.rotation.degrees,
x1: cropRect.left.ceil(),
y1: cropRect.top.ceil(),
x2: cropRect.right.floor(),
y2: cropRect.bottom.floor(),
widget: this,
);
if (context.mounted) {
await BackgroundTaskCrop.addTask(
widget.barcode,
language: widget.language,
imageField: widget.imageField,
imageId: widget.imageId!,
croppedFile: croppedFile,
rotation: _controller.rotation.degrees,
x1: cropRect.left.ceil(),
y1: cropRect.top.ceil(),
x2: cropRect.right.floor(),
y2: cropRect.bottom.floor(),
context: context,
);
}
}
localDatabase.notifyListeners();
if (!mounted) {
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/pages/hunger_games/congrats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ class CongratsWidget extends StatelessWidget {
),
);
if (OpenFoodAPIConfiguration.globalUser != null) {
// ignore: use_build_context_synchronously
if (!context.mounted) {
return;
}
LoadingDialog.run<void>(
context: context,
title: appLocalizations.saving_answer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class _QuestionPageState extends State<_QuestionPage>
barcode: barcode,
insightId: insightId,
insightAnnotation: insightAnnotation,
widget: this,
context: context,
);
}

Expand Down
Loading

0 comments on commit 346c6e3

Please sign in to comment.