From ca45cb39a4b2f16a32f4cb61ed92eb9c38fa64a7 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Tue, 26 Sep 2023 13:13:52 +0300 Subject: [PATCH] Null safety --- lib/redux/credit/credit_actions.dart | 7 +++---- lib/redux/invoice/invoice_actions.dart | 7 +++---- .../purchase_order/purchase_order_actions.dart | 7 +++---- lib/redux/quote/quote_actions.dart | 7 +++---- lib/ui/client/client_pdf.dart | 8 ++++++-- lib/ui/invoice/edit/invoice_edit_items_desktop.dart | 7 +++---- lib/ui/invoice/invoice_pdf.dart | 13 ++++++++----- lib/ui/reports/reports_screen.dart | 5 +++-- lib/ui/reports/reports_screen_vm.dart | 7 ++++++- lib/ui/settings/invoice_design.dart | 2 +- 10 files changed, 39 insertions(+), 31 deletions(-) diff --git a/lib/redux/credit/credit_actions.dart b/lib/redux/credit/credit_actions.dart index 478005729..33b5c8aa0 100644 --- a/lib/redux/credit/credit_actions.dart +++ b/lib/redux/credit/credit_actions.dart @@ -688,8 +688,8 @@ Future handleCreditAction(BuildContext context, List credits, final invitation = credit.invitations.first; final url = invitation.downloadLink; store.dispatch(StartSaving()); - final http.Response? response = await (WebClient() - .get(url, '', rawResponse: true) as FutureOr); + final http.Response? response = + await (WebClient().get(url, '', rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; @@ -699,8 +699,7 @@ Future handleCreditAction(BuildContext context, List credits, final data = json.encode( {'ids': creditIds, 'action': EntityAction.bulkPrint.toApiParam()}); final http.Response? response = await (WebClient() - .post(url, state.credentials.token, data: data, rawResponse: true) - as FutureOr); + .post(url, state.credentials.token, data: data, rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; diff --git a/lib/redux/invoice/invoice_actions.dart b/lib/redux/invoice/invoice_actions.dart index 430eed6cf..b92cd84f1 100644 --- a/lib/redux/invoice/invoice_actions.dart +++ b/lib/redux/invoice/invoice_actions.dart @@ -842,8 +842,8 @@ void handleInvoiceAction(BuildContext? context, List invoices, final invitation = invoice.invitations.first; final url = invitation.downloadLink; store.dispatch(StartSaving()); - final http.Response? response = await (WebClient() - .get(url, '', rawResponse: true) as FutureOr); + final http.Response? response = + await (WebClient().get(url, '', rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; @@ -853,8 +853,7 @@ void handleInvoiceAction(BuildContext? context, List invoices, final data = json.encode( {'ids': invoiceIds, 'action': EntityAction.bulkPrint.toApiParam()}); final http.Response? response = await (WebClient() - .post(url, state.credentials.token, data: data, rawResponse: true) - as FutureOr); + .post(url, state.credentials.token, data: data, rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; diff --git a/lib/redux/purchase_order/purchase_order_actions.dart b/lib/redux/purchase_order/purchase_order_actions.dart index 21336afb1..093bf4a27 100644 --- a/lib/redux/purchase_order/purchase_order_actions.dart +++ b/lib/redux/purchase_order/purchase_order_actions.dart @@ -620,8 +620,8 @@ void handlePurchaseOrderAction(BuildContext? context, final invitation = purchaseOrder!.invitations.first; final url = invitation.downloadLink; store.dispatch(StartSaving()); - final http.Response? response = await (WebClient() - .get(url, '', rawResponse: true) as FutureOr); + final http.Response? response = + await (WebClient().get(url, '', rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; @@ -633,8 +633,7 @@ void handlePurchaseOrderAction(BuildContext? context, 'action': EntityAction.bulkPrint.toApiParam() }); final http.Response? response = await (WebClient() - .post(url, state.credentials.token, data: data, rawResponse: true) - as FutureOr); + .post(url, state.credentials.token, data: data, rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; diff --git a/lib/redux/quote/quote_actions.dart b/lib/redux/quote/quote_actions.dart index 985167e89..6cf1df9e5 100644 --- a/lib/redux/quote/quote_actions.dart +++ b/lib/redux/quote/quote_actions.dart @@ -741,8 +741,8 @@ Future handleQuoteAction(BuildContext context, List quotes, final invitation = quote!.invitations.first; final url = invitation.downloadLink; store.dispatch(StartSaving()); - final http.Response? response = await (WebClient() - .get(url, '', rawResponse: true) as FutureOr); + final http.Response? response = + await (WebClient().get(url, '', rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; @@ -752,8 +752,7 @@ Future handleQuoteAction(BuildContext context, List quotes, final data = json.encode( {'ids': quoteIds, 'action': EntityAction.bulkPrint.toApiParam()}); final http.Response? response = await (WebClient() - .post(url, state.credentials.token, data: data, rawResponse: true) - as FutureOr); + .post(url, state.credentials.token, data: data, rawResponse: true)); store.dispatch(StopSaving()); await Printing.layoutPdf(onLayout: (_) => response!.bodyBytes); break; diff --git a/lib/ui/client/client_pdf.dart b/lib/ui/client/client_pdf.dart index 633136069..bcd1afbda 100644 --- a/lib/ui/client/client_pdf.dart +++ b/lib/ui/client/client_pdf.dart @@ -171,7 +171,7 @@ class _ClientPdfViewState extends State { state.credentials.token, data: data, rawResponse: true, - ) as FutureOr); + )); if (response!.statusCode >= 400) { String errorMessage = @@ -356,8 +356,12 @@ class _ClientPdfViewState extends State { } else { final directory = await (isDesktopOS() ? getDownloadsDirectory() - as FutureOr : getApplicationDocumentsDirectory()); + + if (directory == null) { + return; + } + String filePath = '${directory.path}${file.Platform.pathSeparator}$fileName'; diff --git a/lib/ui/invoice/edit/invoice_edit_items_desktop.dart b/lib/ui/invoice/edit/invoice_edit_items_desktop.dart index 0aeb0b65e..7d298750b 100644 --- a/lib/ui/invoice/edit/invoice_edit_items_desktop.dart +++ b/lib/ui/invoice/edit/invoice_edit_items_desktop.dart @@ -654,6 +654,7 @@ class _InvoiceEditItemsDesktopState extends State { final options = productIds .map((productId) => productState.map[productId]) + .whereType() .where((product) { final filter = textEditingValue.text.toLowerCase(); @@ -678,10 +679,8 @@ class _InvoiceEditItemsDesktopState extends State { return []; } - return options - as FutureOr>; - } as FutureOr> Function( - TextEditingValue), + return options; + }, displayStringForOption: (product) => product.productKey, onSelected: (product) { diff --git a/lib/ui/invoice/invoice_pdf.dart b/lib/ui/invoice/invoice_pdf.dart index c87e5d6f4..bd1141835 100644 --- a/lib/ui/invoice/invoice_pdf.dart +++ b/lib/ui/invoice/invoice_pdf.dart @@ -271,8 +271,12 @@ class _InvoicePdfViewState extends State { } else { final directory = await (isDesktopOS() ? getDownloadsDirectory() - as FutureOr : getApplicationDocumentsDirectory()); + + if (directory == null) { + return; + } + String filePath = '${directory.path}${file.Platform.pathSeparator}$fileName'; @@ -341,13 +345,12 @@ Future _loadPDF( final url = isDeliveryNote ? '/invoices/${invoice.id}/delivery_note' : '/activities/download_entity/$activityId'; - response = await (WebClient().get('${credential.url}$url', credential.token, - rawResponse: true) as FutureOr); + response = await (WebClient() + .get('${credential.url}$url', credential.token, rawResponse: true)); } else { final invitation = invoice.invitations.first; final url = invitation.downloadLink; - response = await (WebClient().get(url, '', rawResponse: true) - as FutureOr); + response = await (WebClient().get(url, '', rawResponse: true)); } if (response!.statusCode >= 400) { diff --git a/lib/ui/reports/reports_screen.dart b/lib/ui/reports/reports_screen.dart index e02da88b9..e540a067e 100644 --- a/lib/ui/reports/reports_screen.dart +++ b/lib/ui/reports/reports_screen.dart @@ -1227,11 +1227,12 @@ class ReportResult { .trim() .isNotEmpty) .map((row) => row[index].renderText(context, column)) + .whereType() .toSet() .toList(); - return options as FutureOr>; - } as FutureOr> Function(TextEditingValue), + return options; + }, onSelected: (value) { final textEditingController = textEditingControllers[column]!; textEditingController.text = value; diff --git a/lib/ui/reports/reports_screen_vm.dart b/lib/ui/reports/reports_screen_vm.dart index aba1cfe80..fa07a9417 100644 --- a/lib/ui/reports/reports_screen_vm.dart +++ b/lib/ui/reports/reports_screen_vm.dart @@ -513,8 +513,13 @@ class ReportsScreenVM { WebUtils.downloadTextFile(filename, csvData); } else { final directory = await (isDesktopOS() - ? getDownloadsDirectory() as FutureOr + ? getDownloadsDirectory() : getApplicationDocumentsDirectory()); + + if (directory == null) { + return; + } + final filePath = directory.path + file.Platform.pathSeparator + filename; final csvFile = file.File(filePath); diff --git a/lib/ui/settings/invoice_design.dart b/lib/ui/settings/invoice_design.dart index 047f7533c..bcdcdcb17 100644 --- a/lib/ui/settings/invoice_design.dart +++ b/lib/ui/settings/invoice_design.dart @@ -1311,7 +1311,7 @@ class _PdfPreviewState extends State<_PdfPreview> { ) .catchError((dynamic error) { print('## Error: $error'); - }) as FutureOr); + })); setState(() => isLoading = false); }