From 376153e22f22fddd091c53d7ef6427b1236a8d41 Mon Sep 17 00:00:00 2001 From: Phaung Phaung Date: Fri, 10 Sep 2021 15:23:13 +0630 Subject: [PATCH] null safety --- lib/pages/delivery/delivery_info.dart | 18 ++++++------- lib/pages/delivery/model/delivery_model.dart | 2 +- .../delivery_address_editor.dart | 12 ++++----- lib/pages/discount/discount_editor.dart | 12 ++++----- lib/pages/faq/faq_detail_page.dart | 3 ++- lib/pages/faq/faq_edit_page.dart | 14 +++++------ .../fcs_shipment/fcs_shipment_editor.dart | 19 ++++++++------ lib/pages/fcs_shipment/fcs_shipment_info.dart | 25 +++++++++++-------- .../fcs_shipment/fcs_shipment_list_row.dart | 8 +++--- .../main/initial_language_selection.dart | 2 +- lib/pages/staff/staff_editor.dart | 14 +++++------ lib/pages/staff/staff_list.dart | 4 +-- lib/pages/user_search/user_list_row.dart | 6 ++--- pubspec.lock | 7 ++++++ 14 files changed, 80 insertions(+), 66 deletions(-) diff --git a/lib/pages/delivery/delivery_info.dart b/lib/pages/delivery/delivery_info.dart index ff3a4d2..20746c9 100644 --- a/lib/pages/delivery/delivery_info.dart +++ b/lib/pages/delivery/delivery_info.dart @@ -60,9 +60,8 @@ class _DeliveryInfoState extends State { @override void initState() { super.initState(); - if(widget.box != null) - _box = widget.box!; - _selectedCartonType = _box.cartonType; + if (widget.box != null) _box = widget.box!; + _selectedCartonType = _box.cartonType ?? ''; //for shipment weight volumetricRatio = Provider.of(context, listen: false) @@ -81,7 +80,7 @@ class _DeliveryInfoState extends State { _heightController.text = _box.height.toString(); _lengthController.text = _box.length.toString(); _cargoTypes = _box.cargoTypes; - _deliveryAddress = _box.deliveryAddress; + _deliveryAddress = _box.deliveryAddress!; isMixBox = _box.cartonType == carton_mix_box; isFromShipments = _box.cartonType == carton_from_shipments; isFromPackages = _box.cartonType == carton_from_packages; @@ -97,7 +96,7 @@ class _DeliveryInfoState extends State { if (_box.cartonType == carton_from_packages && _box.userID == null) return; PackageModel packageModel = Provider.of(context, listen: false); - List packages = await packageModel.getPackages(_box.userID, [ + List packages = await packageModel.getPackages(_box.userID!, [ package_processed_status, package_packed_status, package_shipped_status, @@ -136,8 +135,9 @@ class _DeliveryInfoState extends State { final cartonTypeBox = LocalRadioButtons( readOnly: true, values: cartonModel.cartonTypesInfo, - selectedValue: - _box.isShipmentCarton ? carton_from_shipments : _box.cartonType); + selectedValue: _box.isShipmentCarton ?? false + ? carton_from_shipments + : _box.cartonType); final shipmentBox = DisplayText( text: _box.fcsShipmentNumber, labelTextKey: "box.fcs_shipment_num", @@ -174,11 +174,11 @@ class _DeliveryInfoState extends State { children: [ Expanded( child: new Text( - _selectedShipmentBox.shipmentNumber, + _selectedShipmentBox.shipmentNumber ?? "", style: textStyle, )), new Text( - _selectedShipmentBox.desc, + _selectedShipmentBox.desc ?? "", style: textStyle, ), ], diff --git a/lib/pages/delivery/model/delivery_model.dart b/lib/pages/delivery/model/delivery_model.dart index b200684..ac89a7b 100644 --- a/lib/pages/delivery/model/delivery_model.dart +++ b/lib/pages/delivery/model/delivery_model.dart @@ -42,7 +42,7 @@ class DeliveryModel extends BaseModel { if (listener != null) listener.cancel(); _cartons = []; try { - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection("$path") .where("status", isEqualTo: carton_shipped_status) .where("carton_type", whereIn: [ diff --git a/lib/pages/delivery_address/delivery_address_editor.dart b/lib/pages/delivery_address/delivery_address_editor.dart index 6bb5be1..aa84ba2 100644 --- a/lib/pages/delivery_address/delivery_address_editor.dart +++ b/lib/pages/delivery_address/delivery_address_editor.dart @@ -39,12 +39,12 @@ class _DeliveryAddressEditorState extends State { if (widget.deliveryAddress != null) { _isNew = false; _deliveryAddress = widget.deliveryAddress!; - _nameController.text = _deliveryAddress.fullName; - _address1Controller.text = _deliveryAddress.addressLine1; - _address2Controller.text = _deliveryAddress.addressLine2; - _cityController.text = _deliveryAddress.city; - _stateController.text = _deliveryAddress.state; - _phoneController.text = _deliveryAddress.phoneNumber; + _nameController.text = _deliveryAddress.fullName ?? ""; + _address1Controller.text = _deliveryAddress.addressLine1 ?? ""; + _address2Controller.text = _deliveryAddress.addressLine2 ?? ""; + _cityController.text = _deliveryAddress.city ?? ""; + _stateController.text = _deliveryAddress.state ?? ""; + _phoneController.text = _deliveryAddress.phoneNumber?? ""; } else { _cityController.text = "Yangon"; _stateController.text = "Yangon"; diff --git a/lib/pages/discount/discount_editor.dart b/lib/pages/discount/discount_editor.dart index 1422dee..3b66baf 100644 --- a/lib/pages/discount/discount_editor.dart +++ b/lib/pages/discount/discount_editor.dart @@ -37,11 +37,11 @@ class _DiscountEditorState extends State { super.initState(); if (widget.discount != null) { _discount = widget.discount!; - _codeController.text = _discount.code; + _codeController.text = _discount.code ?? ""; _amountController.text = _discount.amount.toStringAsFixed(2); - _statusController.text = _discount.status; - customerName = _discount.customerName; - customerId = _discount.customerId; + _statusController.text = _discount.status ?? ''; + customerName = _discount.customerName ?? ""; + customerId = _discount.customerId ?? ""; } else { _isNew = true; } @@ -77,8 +77,8 @@ class _DiscountEditorState extends State { icon: Icon(Icons.search, color: primaryColor), onPressed: () => searchUser(context, onUserSelect: (u) { setState(() { - customerId = u.id; - customerName = u.name; + customerId = u.id ?? ""; + customerName = u.name ?? ""; }); },popPage: true)), ], diff --git a/lib/pages/faq/faq_detail_page.dart b/lib/pages/faq/faq_detail_page.dart index 9e7ea4b..d2bd99c 100644 --- a/lib/pages/faq/faq_detail_page.dart +++ b/lib/pages/faq/faq_detail_page.dart @@ -28,7 +28,8 @@ class _FAQDetailPageState extends State { @override Widget build(BuildContext context) { - faq = context.select((FAQModel m) => m.getFAQ(widget.faq.id)); + if(widget.faq.id != null) + faq = context.select((FAQModel m) => m.getFAQ(widget.faq.id!)); if (faq == null) return Text("Deleted"); bool isEditable = context.select((MainModel m) => m.faqEditable()); diff --git a/lib/pages/faq/faq_edit_page.dart b/lib/pages/faq/faq_edit_page.dart index 885741c..d56471c 100644 --- a/lib/pages/faq/faq_edit_page.dart +++ b/lib/pages/faq/faq_edit_page.dart @@ -44,13 +44,13 @@ class _FAQEditorState extends State { if (widget.faq != null) { _faq = widget.faq!; _sn.text = _faq.sn.toString(); - _engQ.text = _faq.questionEng; - _mmQ.text = _faq.questionMm; - _engA.text = _faq.answerEng; - _mmA.text = _faq.answerMm; - _pageLabelEng.text = _faq.pageLinkLabelEng; - _pageLabelMm.text = _faq.pageLinkLabelMm; - _pageLink = _faq.pageLink; + _engQ.text = _faq.questionEng ?? ""; + _mmQ.text = _faq.questionMm ?? ''; + _engA.text = _faq.answerEng ?? ''; + _mmA.text = _faq.answerMm ?? ''; + _pageLabelEng.text = _faq.pageLinkLabelEng ?? ""; + _pageLabelMm.text = _faq.pageLinkLabelMm ?? ""; + _pageLink = _faq.pageLink ?? ''; } } diff --git a/lib/pages/fcs_shipment/fcs_shipment_editor.dart b/lib/pages/fcs_shipment/fcs_shipment_editor.dart index 006ad94..e14fed6 100644 --- a/lib/pages/fcs_shipment/fcs_shipment_editor.dart +++ b/lib/pages/fcs_shipment/fcs_shipment_editor.dart @@ -49,16 +49,19 @@ class _FcsShipmentEditorState extends State { _isNew = widget.shipment == null; if (widget.shipment != null) { _shipment = widget.shipment!; - _shipmentNumberController.text = _shipment.shipmentNumber; - _cutoffDateController.text = dateFormatter.format(_shipment.cutoffDate); - _arrivalDateController.text = dateFormatter.format(_shipment.arrivalDate); + _shipmentNumberController.text = _shipment.shipmentNumber ?? ""; + if(_shipment.cutoffDate != null) + _cutoffDateController.text = dateFormatter.format(_shipment.cutoffDate!); + if(_shipment.arrivalDate != null) + _arrivalDateController.text = dateFormatter.format(_shipment.arrivalDate!); + if(_shipment.departureDate != null) _departureDateControler.text = - dateFormatter.format(_shipment.departureDate); - _statusController.text = _shipment.status; + dateFormatter.format(_shipment.departureDate!); + _statusController.text = _shipment.status ?? ""; _currentShipmentType = _shipment.shipType; - _consigneeController.text = _shipment.consignee; - _portController.text = _shipment.port; - _destinationController.text = _shipment.destination; + _consigneeController.text = _shipment.consignee ?? ""; + _portController.text = _shipment.port ?? ""; + _destinationController.text = _shipment.destination ?? ""; } else { var mainModel = Provider.of(context, listen: false); _currentShipmentType = mainModel.setting.shipmentTypes[0]; diff --git a/lib/pages/fcs_shipment/fcs_shipment_info.dart b/lib/pages/fcs_shipment/fcs_shipment_info.dart index 3635571..ffd7723 100644 --- a/lib/pages/fcs_shipment/fcs_shipment_info.dart +++ b/lib/pages/fcs_shipment/fcs_shipment_info.dart @@ -48,17 +48,20 @@ class _FcsShipmentInfoState extends State { } _load() { - _shipmentNumberController.text = _fcsShipment!.shipmentNumber; - _cutoffDateController.text = dateFormatter.format(_fcsShipment!.cutoffDate); + _shipmentNumberController.text = _fcsShipment!.shipmentNumber ?? ""; + if(_fcsShipment!.cutoffDate != null) + _cutoffDateController.text = dateFormatter.format(_fcsShipment!.cutoffDate!); + if(_fcsShipment!.arrivalDate != null) _arrivalDateController.text = - dateFormatter.format(_fcsShipment!.arrivalDate); + dateFormatter.format(_fcsShipment!.arrivalDate!); + if(_fcsShipment!.departureDate != null) _departureDateControler.text = - dateFormatter.format(_fcsShipment!.departureDate); - _shipmentTypeControler.text = _fcsShipment!.shipType; - _consigneeController.text = _fcsShipment!.consignee; - _portController.text = _fcsShipment!.port; - _destinationController.text = _fcsShipment!.destination; - _statusController.text = _fcsShipment!.status; + dateFormatter.format(_fcsShipment!.departureDate!); + _shipmentTypeControler.text = _fcsShipment!.shipType ?? ""; + _consigneeController.text = _fcsShipment!.consignee ?? ""; + _portController.text = _fcsShipment!.port ?? ""; + _destinationController.text = _fcsShipment!.destination ?? ""; + _statusController.text = _fcsShipment!.status ?? ""; } @override @@ -182,6 +185,7 @@ class _FcsShipmentInfoState extends State { } _edit() async { + var f; bool? updated = await Navigator.push( context, CupertinoPageRoute( @@ -189,7 +193,8 @@ class _FcsShipmentInfoState extends State { ); if (updated ?? false) { var shipmentModel = Provider.of(context, listen: false); - var f = await shipmentModel.getFcsShipment(_fcsShipment!.id); + if(_fcsShipment != null && _fcsShipment!.id != null ) + f = await shipmentModel.getFcsShipment(_fcsShipment!.id!); setState(() { _fcsShipment = f; }); diff --git a/lib/pages/fcs_shipment/fcs_shipment_list_row.dart b/lib/pages/fcs_shipment/fcs_shipment_list_row.dart index 3a6da34..b16e410 100644 --- a/lib/pages/fcs_shipment/fcs_shipment_list_row.dart +++ b/lib/pages/fcs_shipment/fcs_shipment_list_row.dart @@ -43,9 +43,7 @@ class FcsShipmentListRow extends StatelessWidget { Padding( padding: const EdgeInsets.only(left: 8.0), child: new Text( - shipment!.shipmentNumber == null - ? '' - : shipment!.shipmentNumber, + shipment?.shipmentNumber ?? '', style: new TextStyle( fontSize: 15.0, color: Colors.black), ), @@ -53,7 +51,7 @@ class FcsShipmentListRow extends StatelessWidget { Padding( padding: const EdgeInsets.only(left: 10.0, top: 10), child: new Text( - dateFormatter.format(shipment!.cutoffDate), + dateFormatter.format(shipment!.cutoffDate!), style: new TextStyle( fontSize: 15.0, color: Colors.grey), ), @@ -67,7 +65,7 @@ class FcsShipmentListRow extends StatelessWidget { ), Padding( padding: const EdgeInsets.all(0), - child: getStatus(shipment!.status), + child: getStatus(shipment!.status ?? ''), ), ], ), diff --git a/lib/pages/main/initial_language_selection.dart b/lib/pages/main/initial_language_selection.dart index 0253db3..1dd0864 100644 --- a/lib/pages/main/initial_language_selection.dart +++ b/lib/pages/main/initial_language_selection.dart @@ -177,7 +177,7 @@ class _InitialLanguageSelectionPageState setState(() { selectedIndex = index; selectedLanguage = lang; - Translation().onLocaleChanged!(Locale(languagesMap[lang])); + Translation().onLocaleChanged(Locale(languagesMap[lang])); Provider.of(context, listen: false) .saveLanguage(selectedLanguage); }); diff --git a/lib/pages/staff/staff_editor.dart b/lib/pages/staff/staff_editor.dart index 6cf876c..5a703b0 100644 --- a/lib/pages/staff/staff_editor.dart +++ b/lib/pages/staff/staff_editor.dart @@ -56,7 +56,7 @@ class _StaffEditorState extends State { title: InkWell( onTap: () { setState(() { - p.isChecked = p.isChecked == null ? true : !p.isChecked; + p.isChecked = p.isChecked == null ? true : !p.isChecked!; }); }, child: new Row( @@ -79,10 +79,10 @@ class _StaffEditorState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ new Text( - p.name, + p.name ??"", style: TextStyle(fontSize: 15.0, color: primaryColor), ), - Text(p.desc, + Text(p.desc ?? "", style: TextStyle(fontSize: 13, color: Colors.grey[600])) ], ), @@ -151,7 +151,7 @@ class _StaffEditorState extends State { ? Container() : IconButton( icon: Icon(Icons.open_in_new, color: primaryColor), - onPressed: () => call(context, user.phoneNumber)), + onPressed: () => call(context, user.phoneNumber!)), ], ); @@ -227,7 +227,7 @@ class _StaffEditorState extends State { }); StaffModel staffModel = Provider.of(context, listen: false); try { - await staffModel.updatePrivileges(this.selectedUser!.id, privilegesIDs()); + await staffModel.updatePrivileges(this.selectedUser!.id!, privilegesIDs()); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); @@ -239,7 +239,7 @@ class _StaffEditorState extends State { } List privilegesIDs() { - return this.privileges.where((p) => p.isChecked).map((p) => p.id).toList(); + return this.privileges.where((p) => p.isChecked!).map((p) => p.id).toList(); } _save() async { @@ -249,7 +249,7 @@ class _StaffEditorState extends State { if (widget.staff == null) return; StaffModel staffModel = Provider.of(context, listen: false); try { - await staffModel.updatePrivileges(widget.staff!.id, privilegesIDs()); + await staffModel.updatePrivileges(widget.staff!.id!, privilegesIDs()); Navigator.pop(context); } catch (e) { showMsgDialog(context, "Error", e.toString()); diff --git a/lib/pages/staff/staff_list.dart b/lib/pages/staff/staff_list.dart index 0fe6735..11fd119 100644 --- a/lib/pages/staff/staff_list.dart +++ b/lib/pages/staff/staff_list.dart @@ -93,13 +93,13 @@ class _StaffListState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ new Text( - user.name, + user.name ?? "", style: new TextStyle(fontSize: 15.0), ), Padding( padding: const EdgeInsets.only(top: 8.0), child: new Text( - user.phoneNumber, + user.phoneNumber ?? "", style: new TextStyle( fontSize: 15.0, color: Colors.grey), ), diff --git a/lib/pages/user_search/user_list_row.dart b/lib/pages/user_search/user_list_row.dart index 2fb7b74..5760047 100644 --- a/lib/pages/user_search/user_list_row.dart +++ b/lib/pages/user_search/user_list_row.dart @@ -53,17 +53,17 @@ class _UserListRowState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ new Text( - user.name == null ? '' : user.name, + user.name ?? "", style: new TextStyle( fontSize: 15.0, color: Colors.black), ), new Text( - user.fcsID == null ? "" : user.fcsID, + user.fcsID ?? "", style: new TextStyle( fontSize: 13.0, color: Colors.grey), ), new Text( - user.phoneNumber == null ? "" : user.phoneNumber, + user.phoneNumber ?? "", style: new TextStyle( fontSize: 13.0, color: Colors.grey), ), diff --git a/pubspec.lock b/pubspec.lock index 2abfb07..20ac1cb 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -349,6 +349,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.1" + flutter_icons_null_safety: + dependency: "direct main" + description: + name: flutter_icons_null_safety + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" flutter_local_notifications: dependency: "direct main" description: