From 2c95ec7600f7c51076d6d7b1db107b05740eb6d6 Mon Sep 17 00:00:00 2001 From: phyothandar Date: Fri, 10 Sep 2021 17:14:59 +0630 Subject: [PATCH] null safety --- lib/domain/entities/invoice.dart | 10 ++++----- lib/domain/entities/package.dart | 2 +- lib/helpers/firebase_helper.dart | 15 ++++++++----- lib/pages/carton/cargo_type_editor.dart | 2 +- lib/pages/carton_search/carton_search.dart | 2 +- lib/pages/invoice/editor/invoice_editor.dart | 16 +++++++------- .../editor/invoice_handling_fee_list.dart | 4 ++-- lib/pages/invoice/invoice_info.dart | 4 ++-- lib/pages/invoice/invoice_table.dart | 4 ++-- lib/pages/package/model/package_model.dart | 22 +++++++++---------- lib/pages/package/tracking_id_page.dart | 2 +- lib/pages/package_search/package_serach.dart | 2 +- lib/pages/profile/profile_page.dart | 2 +- lib/pages/rates/custom_editor.dart | 2 +- lib/pages/rates/custom_list.dart | 2 +- lib/pages/rates/shipment_rates_calculate.dart | 4 ++-- lib/pages/receiving/receiving_editor.dart | 2 +- lib/pages/shipment/shipment_assign.dart | 2 +- lib/pages/shipment/shipment_confirm.dart | 2 +- lib/pages/shipment/shipment_info.dart | 2 +- lib/pages/widgets/multi_img_controller.dart | 2 +- 21 files changed, 54 insertions(+), 51 deletions(-) diff --git a/lib/domain/entities/invoice.dart b/lib/domain/entities/invoice.dart index 3ac1c9e..a15e905 100644 --- a/lib/domain/entities/invoice.dart +++ b/lib/domain/entities/invoice.dart @@ -28,7 +28,7 @@ class Invoice { List customDuties; List cartons; List cargoTypes; - List shipments; + List? shipments; List payments; Discount? discount; PaymentMethod? paymentMethod; @@ -85,9 +85,9 @@ class Invoice { } double getHandlingFee() { - return shipments - .where((sh) => sh.isSelected) - .fold(0, (p, s) => p + (s.handlingFee - s.paidHandlingFee)); + return shipments! + .where((sh) => sh!.isSelected) + .fold(0, (p, s) => p + (s!.handlingFee - s.paidHandlingFee)); } double getTotalBalance(Rate rate) { @@ -176,7 +176,7 @@ class Invoice { List _cargoTypes = cargoTypes.map((c) => c.toMap()).toList(); List _customDuties = customDuties.map((c) => c.toMap()).toList(); List _cartons = cartons.map((c) => c.toMap()).toList(); - List _shipments = shipments.map((s) => s.toMap()).toList(); + List _shipments = shipments!.map((s) => s!.toMap()).toList(); return { "id": id, "invoice_date": invoiceDate?.toUtc().toIso8601String(), diff --git a/lib/domain/entities/package.dart b/lib/domain/entities/package.dart index c583de1..2df0c7d 100644 --- a/lib/domain/entities/package.dart +++ b/lib/domain/entities/package.dart @@ -36,7 +36,7 @@ class Package { DeliveryAddress? deliveryAddress; //for packages in processing - List photoFiles; + List photoFiles; int get amount => rate != null && weight != null ? rate * weight : 0; diff --git a/lib/helpers/firebase_helper.dart b/lib/helpers/firebase_helper.dart index 0fd5b84..4014aa8 100644 --- a/lib/helpers/firebase_helper.dart +++ b/lib/helpers/firebase_helper.dart @@ -24,20 +24,24 @@ Future getClaims({bool refreshIdToken = false}) async { } // returns list of url -Future> uploadFiles(String path, List files, +Future> uploadFiles(String path, List files, {String? fileName}) async { List> fu = []; - for (File f in files) { + for (File? f in files) { Future u = uploadStorage(path, f); fu.add(u); } return Future.wait(fu); } -Future uploadStorage(String path, File file, {String? fileName}) async { +Future uploadStorage(String path, File? file, + {String? fileName}) async { if (fileName == null) { fileName = Uuid().v4(); } + if (file == null) { + return Future.value(''); + } Reference ref = FirebaseStorage.instance.ref().child('$path/$fileName'); UploadTask uploadTask = ref.putFile(file); await uploadTask.resume(); @@ -57,10 +61,11 @@ Future uploadStorage(String path, File file, {String? fileName}) async { // return downloadUrl; } -Future deleteStorageFromUrls(List urls) async { +Future deleteStorageFromUrls(List urls) async { if (urls == null) return; for (int i = 0; i < urls.length; i++) { - await deleteStorageFromUrl(urls[i]); + if (urls[i] == null) return; + await deleteStorageFromUrl(urls[i]!); } } diff --git a/lib/pages/carton/cargo_type_editor.dart b/lib/pages/carton/cargo_type_editor.dart index 7c8553a..1d6572c 100644 --- a/lib/pages/carton/cargo_type_editor.dart +++ b/lib/pages/carton/cargo_type_editor.dart @@ -39,7 +39,7 @@ class _CargoTypeEditorState extends State { _loadDefalut() { ShipmentRateModel shipmentRateModel = Provider.of(context, listen: false); - _cargo = shipmentRateModel.rate.defaultCargoType?.clone(); + _cargo = shipmentRateModel.rate.defaultCargoType.clone(); } @override diff --git a/lib/pages/carton_search/carton_search.dart b/lib/pages/carton_search/carton_search.dart index 5442616..07c2215 100644 --- a/lib/pages/carton_search/carton_search.dart +++ b/lib/pages/carton_search/carton_search.dart @@ -171,7 +171,7 @@ class PartSearchDelegate extends SearchDelegate { // } try { - String barcode = await scanBarcode(); + String? barcode = await scanBarcode(); if (barcode != null) { query = barcode; showResults(context); diff --git a/lib/pages/invoice/editor/invoice_editor.dart b/lib/pages/invoice/editor/invoice_editor.dart index c8b9efa..99b5143 100644 --- a/lib/pages/invoice/editor/invoice_editor.dart +++ b/lib/pages/invoice/editor/invoice_editor.dart @@ -103,10 +103,10 @@ class _InvoiceEditorState extends State { _loadShipments() async { ShipmentModel shipmentModel = Provider.of(context, listen: false); - List shipments = await shipmentModel.getShipmentWithHandlingFee( + List? shipments = await shipmentModel.getShipmentWithHandlingFee( widget.fcsShipment!.id!, widget.customer!.id!); - shipments.forEach((s) { - s.isSelected = true; + shipments!.forEach((s) { + s!.isSelected = true; }); setState(() { _invoice!.shipments = shipments; @@ -380,8 +380,8 @@ class _InvoiceEditorState extends State { if (shipment == null) return; shipment.isSelected = true; setState(() { - _invoice!.shipments.remove(shipment); - _invoice!.shipments.add(shipment); + _invoice!.shipments!.remove(shipment); + _invoice!.shipments!.add(shipment); }); } @@ -389,8 +389,8 @@ class _InvoiceEditorState extends State { if (shipment == null) return; shipment.isSelected = false; setState(() { - _invoice!.shipments.remove(shipment); - _invoice!.shipments.add(shipment); + _invoice!.shipments!.remove(shipment); + _invoice!.shipments!.add(shipment); }); } @@ -431,7 +431,7 @@ class _InvoiceEditorState extends State { invoice.handlingFee = _invoice!.getHandlingFee(); invoice.cartons = _invoice!.cartons.where((c) => c.isChecked!).toList(); invoice.shipments = - _invoice!.shipments.where((s) => s.isSelected).toList(); + _invoice!.shipments!.where((s) => s!.isSelected).toList(); invoice.discount = _invoice!.discount; invoice.deliveryFee = _invoice!.deliveryFee; diff --git a/lib/pages/invoice/editor/invoice_handling_fee_list.dart b/lib/pages/invoice/editor/invoice_handling_fee_list.dart index 5ee937d..75d6462 100644 --- a/lib/pages/invoice/editor/invoice_handling_fee_list.dart +++ b/lib/pages/invoice/editor/invoice_handling_fee_list.dart @@ -9,7 +9,7 @@ typedef OnAdd(Shipment shipment); typedef OnRemove(Shipment shipment); class InvoiceHandlingFeeList extends StatelessWidget { - final List? shipments; + final List? shipments; final OnAdd? onAdd; final OnRemove? onRemove; @@ -72,7 +72,7 @@ class InvoiceHandlingFeeList extends StatelessWidget { onSelectChanged: (value) => Navigator.pop(context, c), cells: [ MyDataCell(new Text( - c.shipmentNumber!, + c!.shipmentNumber!, style: textStyle, )), MyDataCell( diff --git a/lib/pages/invoice/invoice_info.dart b/lib/pages/invoice/invoice_info.dart index ba3adbc..d9980ec 100644 --- a/lib/pages/invoice/invoice_info.dart +++ b/lib/pages/invoice/invoice_info.dart @@ -39,8 +39,8 @@ class _InvoiceInfoState extends State { void initState() { super.initState(); _invoice = widget.invoice!; - _invoice!.shipments.forEach((s) { - s.isSelected = true; + _invoice!.shipments!.forEach((s) { + s!.isSelected = true; }); _loadCartons(); } diff --git a/lib/pages/invoice/invoice_table.dart b/lib/pages/invoice/invoice_table.dart index 46ed34b..77812dd 100644 --- a/lib/pages/invoice/invoice_table.dart +++ b/lib/pages/invoice/invoice_table.dart @@ -70,11 +70,11 @@ class InvoiceTable extends StatelessWidget { "${c.calWeight.toStringAsFixed(2)} x ${c.calRate.toStringAsFixed(2)}", amount: "${c.calAmount.toStringAsFixed(2)}")); }); - invoice!.shipments.where((ss) => (ss.isSelected )).forEach((s) { + invoice!.shipments!.where((ss) => (ss!.isSelected )).forEach((s) { tableRows.add(InvoiceTableRow( data: s, invoiceDataType: InvoiceDataType.HandlingFeeType, - desc: "Handling fee\n${s.shipmentNumber}", + desc: "Handling fee\n${s!.shipmentNumber}", rate: "", amount: "${s.handlingFee.toStringAsFixed(2)}")); }); diff --git a/lib/pages/package/model/package_model.dart b/lib/pages/package/model/package_model.dart index 9e979e1..380f9b7 100644 --- a/lib/pages/package/model/package_model.dart +++ b/lib/pages/package/model/package_model.dart @@ -260,7 +260,7 @@ class PackageModel extends BaseModel { } Future createReceiving( - User user, Package package, List files) async { + User user, Package package, List files) async { if (user != null) { package.fcsID = user.fcsID; } @@ -288,22 +288,21 @@ class PackageModel extends BaseModel { } } - Future updateReceiving(User user, Package package, List files, - List deletedUrls) async { + Future updateReceiving(User user, Package package, List files, + List deletedUrls) async { if (user != null) { package.fcsID = user.fcsID; } if (deletedUrls != null) { - for (String url in deletedUrls) { + for (String? url in deletedUrls) { package.photoUrls.remove(url); } } List uploadedURL = []; if (files != null) { - var count = (package.photoUrls?.length ?? 0) + - files.length - - (deletedUrls?.length ?? 0); + var count = + (package.photoUrls.length) + files.length - (deletedUrls.length); if (count > uploadPhotoLimit) throw Exception("Exceed number of file upload"); @@ -333,18 +332,17 @@ class PackageModel extends BaseModel { } Future updateProcessing( - Package package, List files, List deletedUrls) async { + Package package, List files, List deletedUrls) async { if (deletedUrls != null) { - for (String url in deletedUrls) { + for (String? url in deletedUrls) { package.photoUrls.remove(url); } } List uploadedURL = []; if (files != null) { - var count = (package.photoUrls?.length ?? 0) + - files.length - - (deletedUrls?.length ?? 0); + var count = + (package.photoUrls.length) + files.length - (deletedUrls.length); if (count > uploadPhotoLimit) throw Exception("Exceed number of file upload"); diff --git a/lib/pages/package/tracking_id_page.dart b/lib/pages/package/tracking_id_page.dart index 1856f7a..6cdd773 100644 --- a/lib/pages/package/tracking_id_page.dart +++ b/lib/pages/package/tracking_id_page.dart @@ -164,7 +164,7 @@ class _TrackingIDPageState extends State { // } try { - String barcode = await scanBarcode(); + String? barcode = await scanBarcode(); if (barcode != null) { setState(() { _transcationIDCtl.text = barcode; diff --git a/lib/pages/package_search/package_serach.dart b/lib/pages/package_search/package_serach.dart index 18b57ef..566b46c 100644 --- a/lib/pages/package_search/package_serach.dart +++ b/lib/pages/package_search/package_serach.dart @@ -147,7 +147,7 @@ class PackageSearchDelegate extends SearchDelegate { // Barcode bc = barcodes.firstWhere((element) => true); // String barcode; // if (bc != null) barcode = bc.rawValue; - String barcode = await scanBarcode(); + String? barcode = await scanBarcode(); if (barcode != null) { query = barcode; showResults(context); diff --git a/lib/pages/profile/profile_page.dart b/lib/pages/profile/profile_page.dart index a618c3a..3732104 100644 --- a/lib/pages/profile/profile_page.dart +++ b/lib/pages/profile/profile_page.dart @@ -80,7 +80,7 @@ class _ProfileState extends State { ); final phonenumberbox = DisplayText( - text: mainModel.user!.phone ?? "", + text: mainModel.user!.phone, labelTextKey: "profile.phone", iconData: Icons.phone, ); diff --git a/lib/pages/rates/custom_editor.dart b/lib/pages/rates/custom_editor.dart index 4e7f2b1..bec5ddb 100644 --- a/lib/pages/rates/custom_editor.dart +++ b/lib/pages/rates/custom_editor.dart @@ -36,7 +36,7 @@ class _CustomEditorState extends State { _productController.text = _custom.name??""; _feeController.text = _custom.customDutyFee.toStringAsFixed(2); _shipmentRateController.text = - _custom.rate == null ? "" : _custom.rate?.toStringAsFixed(2) ?? ''; + _custom.rate == null ? "" : _custom.rate.toStringAsFixed(2); } else { _isNew = true; } diff --git a/lib/pages/rates/custom_list.dart b/lib/pages/rates/custom_list.dart index 898c8a3..f5a1dc9 100644 --- a/lib/pages/rates/custom_list.dart +++ b/lib/pages/rates/custom_list.dart @@ -86,7 +86,7 @@ class _CustomListState extends State { custom.rate == null ? "" : "Shipment rate \$ " + - custom.rate!.toStringAsFixed(2)), + custom.rate.toStringAsFixed(2)), ), ); }), diff --git a/lib/pages/rates/shipment_rates_calculate.dart b/lib/pages/rates/shipment_rates_calculate.dart index d956a55..c722c86 100644 --- a/lib/pages/rates/shipment_rates_calculate.dart +++ b/lib/pages/rates/shipment_rates_calculate.dart @@ -62,11 +62,11 @@ class _ShipmentRatesCalState extends State { var amount = box.calAmount(rate); var shipmentWeight = box.getShipmentWeight(rate.volumetricRatio); var effectiveWeight = - _cargoType.weight! > shipmentWeight ? _cargoType.weight : shipmentWeight; + _cargoType.weight > shipmentWeight ? _cargoType.weight : shipmentWeight; setState(() { _deliveryFee = - effectiveWeight! > rate.freeDeliveryWeight ? 0 : rate.deliveryFee; + effectiveWeight > rate.freeDeliveryWeight ? 0 : rate.deliveryFee; _amount = amount == null ? 0 : amount + _deliveryFee; _shipmentWeight = shipmentWeight.toDouble(); }); diff --git a/lib/pages/receiving/receiving_editor.dart b/lib/pages/receiving/receiving_editor.dart index e8ec46c..a120ee0 100644 --- a/lib/pages/receiving/receiving_editor.dart +++ b/lib/pages/receiving/receiving_editor.dart @@ -212,7 +212,7 @@ class _ReceivingEditorState extends State { // } try { - String barcode = await scanBarcode(); + String? barcode = await scanBarcode(); if (barcode != null) { setState(() { _trackingIDCtl.text = barcode; diff --git a/lib/pages/shipment/shipment_assign.dart b/lib/pages/shipment/shipment_assign.dart index 7c37695..ad2e808 100644 --- a/lib/pages/shipment/shipment_assign.dart +++ b/lib/pages/shipment/shipment_assign.dart @@ -56,7 +56,7 @@ class _ShipmentAssignState extends State { _selectedShipmentType = _shipment!.shipmentType; _fromTimeEditingController.text = _shipment!.pickupTimeStart!; _toTimeEditingController.text = _shipment!.pickupTimeEnd!; - _pickupDate.text = dateFormatter.format(_shipment!.pickupDate! ?? now); + _pickupDate.text = dateFormatter.format(_shipment!.pickupDate ?? now); _handlingFee.text = _shipment!.handlingFee != null ? _shipment!.handlingFee.toString() : "0"; diff --git a/lib/pages/shipment/shipment_confirm.dart b/lib/pages/shipment/shipment_confirm.dart index 68daf42..05ed394 100644 --- a/lib/pages/shipment/shipment_confirm.dart +++ b/lib/pages/shipment/shipment_confirm.dart @@ -37,7 +37,7 @@ class _ShipmentConfirmState extends State { super.initState(); _shipment = widget.shipment; - _handlingFee.text = _shipment!.handlingFee?.toString() ?? "0"; + _handlingFee.text = _shipment!.handlingFee.toString(); } @override diff --git a/lib/pages/shipment/shipment_info.dart b/lib/pages/shipment/shipment_info.dart index 382d2af..ee29ad5 100644 --- a/lib/pages/shipment/shipment_info.dart +++ b/lib/pages/shipment/shipment_info.dart @@ -223,7 +223,7 @@ class _ShipmentInfoState extends State { iconData: MaterialCommunityIcons.worker); var handlingFeeBox = DisplayText( labelTextKey: "shipment.handling.fee", - text: (_shipment!.handlingFee ?? 0).toString(), + text: (_shipment!.handlingFee).toString(), iconData: FontAwesome.truck); final assignCompleteBtn = LocalButton( diff --git a/lib/pages/widgets/multi_img_controller.dart b/lib/pages/widgets/multi_img_controller.dart index 26191ed..ad0be39 100644 --- a/lib/pages/widgets/multi_img_controller.dart +++ b/lib/pages/widgets/multi_img_controller.dart @@ -80,7 +80,7 @@ class MultiImgController { List get getUpdatedFile { List _addfiles = getAddedFile; - this.imageFiles!.addAll(_addfiles); + this.imageFiles.addAll(_addfiles); return this.imageFiles; }