From 0f84fec2f742c6042a472b3789bc14f8f15def30 Mon Sep 17 00:00:00 2001 From: Phaung Phaung Date: Fri, 10 Sep 2021 16:48:20 +0630 Subject: [PATCH] null safety --- lib/pages/delivery/delivery_info.dart | 2 +- lib/pages/delivery/model/delivery_model.dart | 20 ++++---- .../model/delivery_address_model.dart | 35 +++++++------- lib/pages/discount/model/discount_model.dart | 28 +++++------ lib/pages/faq/model/faq_model.dart | 12 ++--- .../fcs_shipment/fcs_shipment_editor.dart | 6 +-- .../model/fcs_shipment_model.dart | 48 +++++++++---------- .../invoice/editor/invoice_discount_list.dart | 6 +-- lib/pages/invoice/editor/invoice_editor.dart | 2 +- lib/pages/staff/model/staff_model.dart | 36 +++++++------- lib/pages/staff/staff_editor.dart | 2 +- .../widgets/local_popup_menu_button.dart | 12 ++--- 12 files changed, 103 insertions(+), 106 deletions(-) diff --git a/lib/pages/delivery/delivery_info.dart b/lib/pages/delivery/delivery_info.dart index 20746c9..fef0d5e 100644 --- a/lib/pages/delivery/delivery_info.dart +++ b/lib/pages/delivery/delivery_info.dart @@ -135,7 +135,7 @@ class _DeliveryInfoState extends State { final cartonTypeBox = LocalRadioButtons( readOnly: true, values: cartonModel.cartonTypesInfo, - selectedValue: _box.isShipmentCarton ?? false + selectedValue: (_box.isShipmentCarton ?? false) ? carton_from_shipments : _box.cartonType); final shipmentBox = DisplayText( diff --git a/lib/pages/delivery/model/delivery_model.dart b/lib/pages/delivery/model/delivery_model.dart index ac89a7b..235f820 100644 --- a/lib/pages/delivery/model/delivery_model.dart +++ b/lib/pages/delivery/model/delivery_model.dart @@ -14,18 +14,18 @@ class DeliveryModel extends BaseModel { List get cartons => _selectedIndex == 1 ? _cartons : List.from(_delivered.values); - Paginator _delivered; + late Paginator _delivered; int _selectedIndex = 1; bool isLoading = false; List _cartons = []; - StreamSubscription listener; + StreamSubscription? listener; set selectedIndex(int index) { _selectedIndex = index; notifyListeners(); } - get selectedIndex => _selectedIndex; + int get selectedIndex => _selectedIndex; initData() { _selectedIndex = 1; @@ -37,9 +37,9 @@ class DeliveryModel extends BaseModel { } Future _loadCartons() async { - if (user == null || !user.hasDeliveries()) return; + if (user == null || !user!.hasDeliveries()) return; String path = "/$cartons_collection/"; - if (listener != null) listener.cancel(); + if (listener != null) listener!.cancel(); _cartons = []; try { listener = FirebaseFirestore.instance @@ -55,9 +55,9 @@ class DeliveryModel extends BaseModel { .snapshots() .listen((QuerySnapshot snapshot) { _cartons.clear(); - _cartons = snapshot.documents.map((documentSnapshot) { + _cartons = snapshot.docs.map((documentSnapshot) { var s = Carton.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return s; }).toList(); notifyListeners(); @@ -68,9 +68,9 @@ class DeliveryModel extends BaseModel { } Paginator _getDelivered() { - if (user == null || !user.hasDeliveries()) return null; + if (user == null || !user!.hasDeliveries()) throw "No Privilege"; - var pageQuery = Firestore.instance + var pageQuery = FirebaseFirestore.instance .collection("/$cartons_collection") .where("is_delivered", isEqualTo: true) .where("status", whereIn: [carton_delivered_status]).where("is_deleted", @@ -105,7 +105,7 @@ class DeliveryModel extends BaseModel { @override logout() async { - if (listener != null) await listener.cancel(); + if (listener != null) await listener!.cancel(); if (_delivered != null) _delivered.close(); _cartons = []; } diff --git a/lib/pages/delivery_address/model/delivery_address_model.dart b/lib/pages/delivery_address/model/delivery_address_model.dart index a344c82..0ccbf33 100644 --- a/lib/pages/delivery_address/model/delivery_address_model.dart +++ b/lib/pages/delivery_address/model/delivery_address_model.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'package:barcode_scan2/gen/protos/protos.pb.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:fcs/data/services/services.dart'; import 'package:fcs/domain/vo/delivery_address.dart'; @@ -10,13 +11,13 @@ class DeliveryAddressModel extends BaseModel { final log = Logger('FcsShipmentModel'); List deliveryAddresses = []; - StreamSubscription listener; + StreamSubscription? listener; DeliveryAddress get defalutAddress => - deliveryAddresses.firstWhere((e) => e.isDefault, orElse: () => null); + deliveryAddresses.firstWhere((e) => e.isDefault, orElse: () => DeliveryAddress()); DeliveryAddress getLocalDeliveryAddress(String id) => - deliveryAddresses.firstWhere((e) => e.id == id, orElse: () => null); + deliveryAddresses.firstWhere((e) => e.id == id, orElse: () => DeliveryAddress()); @override void privilegeChanged() { @@ -26,26 +27,26 @@ class DeliveryAddressModel extends BaseModel { @override logout() async { - if (listener != null) await listener.cancel(); + if (listener != null) await listener!.cancel(); deliveryAddresses = []; } Future _loadDeliveryAddresses() async { if (user == null) return; String path = "$delivery_address_collection/"; - if (listener != null) listener.cancel(); + if (listener != null) listener!.cancel(); deliveryAddresses = []; try { - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection('users') - .document("${user.id}") + .doc("${user!.id}") .collection("$path") .snapshots() .listen((QuerySnapshot snapshot) { deliveryAddresses.clear(); - deliveryAddresses = snapshot.documents.map((documentSnapshot) { + deliveryAddresses = snapshot.docs.map((documentSnapshot) { var s = DeliveryAddress.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return s; }).toList(); notifyListeners(); @@ -56,9 +57,9 @@ class DeliveryAddressModel extends BaseModel { } Future getDeliveryAddress(String id) async { - String path = "/$user_collection/${user.id}/$delivery_address_collection"; - var snap = await Firestore.instance.collection(path).document(id).get(); - return DeliveryAddress.fromMap(snap.data, snap.documentID); + String path = "/$user_collection/${user!.id}/$delivery_address_collection"; + var snap = await FirebaseFirestore.instance.collection(path).doc(id).get(); + return DeliveryAddress.fromMap(snap.data as Map, snap.id); } void initUser(user) { @@ -87,14 +88,14 @@ class DeliveryAddressModel extends BaseModel { Future> getDeliveryAddresses(String userID) async { String path = "$delivery_address_collection/"; - var querySnap = await Firestore.instance + var querySnap = await FirebaseFirestore.instance .collection('users') - .document("$userID") + .doc("$userID") .collection("$path") .orderBy("full_name") - .getDocuments(); - return querySnap.documents - .map((e) => DeliveryAddress.fromMap(e.data, e.documentID)) + .get(); + return querySnap.docs + .map((e) => DeliveryAddress.fromMap(e.data as Map, e.id)) .toList(); } } diff --git a/lib/pages/discount/model/discount_model.dart b/lib/pages/discount/model/discount_model.dart index 445853f..df50e03 100644 --- a/lib/pages/discount/model/discount_model.dart +++ b/lib/pages/discount/model/discount_model.dart @@ -30,9 +30,7 @@ class DiscountModel extends BaseModel { initData() { _selectedIndex = 1; _load(); - - if (_used != null) _used.close(); - if (_getUsed() != null) _used = _getUsed()!; + if (_getUsed() != null) _used = _getUsed(); _used.load(); } @@ -44,14 +42,14 @@ class DiscountModel extends BaseModel { _load() { if (listener != null) listener!.cancel(); try { - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection("/$discounts_collection") .orderBy("code", descending: false) .snapshots() .listen((snaps) { _discounts.clear(); - snaps.documents.forEach((d) { - _discounts.add(Discount.fromMap(d.data, d.documentID)); + snaps.docs.forEach((d) { + _discounts.add(Discount.fromMap(d.data as Map, d.id)); }); notifyListeners(); }); @@ -60,10 +58,10 @@ class DiscountModel extends BaseModel { } } - Paginator? _getUsed() { - if (user == null || !user.hasFcsShipments()) return null; + Paginator _getUsed() { + if (user == null || !user!.hasFcsShipments()) throw "No Privilege"; - var pageQuery = Firestore.instance + var pageQuery = FirebaseFirestore.instance .collection("/$discounts_collection") .where("status", isEqualTo: fcs_shipment_shipped_status) .orderBy("code", descending: false); @@ -73,17 +71,17 @@ class DiscountModel extends BaseModel { return paginator; } - Future?> getDiscount(String userID) async { + Future?> getDiscount(String userID) async { String path = "/$discounts_collection"; try { - var q = Firestore.instance + var q = FirebaseFirestore.instance .collection("$path") .where("customer_id", isEqualTo: userID) .where("status", isEqualTo: "available"); - var snaps = await q.getDocuments(source: Source.server); - List discounts = snaps.documents.map((snap) { + var snaps = await q.get(const GetOptions(source: Source.server)); + var discounts = snaps.docs.map((snap) { if (snap.exists) { - var s = Discount.fromMap(snap.data, snap.documentID); + var s = Discount.fromMap(snap.data as Map, snap.id); return s; } }).toList(); @@ -128,6 +126,6 @@ class DiscountModel extends BaseModel { } Future deleteDiscount(Discount discount) async { - return Services.instance.commonService.deleteDiscount(discount.id); + return Services.instance.commonService.deleteDiscount(discount.id!); } } diff --git a/lib/pages/faq/model/faq_model.dart b/lib/pages/faq/model/faq_model.dart index ece61b7..cc43d41 100644 --- a/lib/pages/faq/model/faq_model.dart +++ b/lib/pages/faq/model/faq_model.dart @@ -12,22 +12,22 @@ class FAQModel extends BaseModel { List faqs = []; FAQ getFAQ(String id) { - return faqs.firstWhere((e) => e.id == id, orElse: () => null); + return faqs.firstWhere((e) => e.id == id, orElse: () => FAQ()); } - StreamSubscription listener; + StreamSubscription? listener; FAQModel() { - if (listener != null) listener.cancel(); + if (listener != null) listener!.cancel(); try { - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection("/faqs") .orderBy("sn", descending: false) .snapshots() .listen((snaps) { faqs.clear(); - snaps.documents.forEach((d) { - faqs.add(FAQ.fromMap(d.data, d.documentID)); + snaps.docs.forEach((d) { + faqs.add(FAQ.fromMap(d.data as Map, d.id)); }); notifyListeners(); }); diff --git a/lib/pages/fcs_shipment/fcs_shipment_editor.dart b/lib/pages/fcs_shipment/fcs_shipment_editor.dart index e14fed6..0a48598 100644 --- a/lib/pages/fcs_shipment/fcs_shipment_editor.dart +++ b/lib/pages/fcs_shipment/fcs_shipment_editor.dart @@ -64,7 +64,7 @@ class _FcsShipmentEditorState extends State { _destinationController.text = _shipment.destination ?? ""; } else { var mainModel = Provider.of(context, listen: false); - _currentShipmentType = mainModel.setting.shipmentTypes[0]; + _currentShipmentType = mainModel.setting!.shipmentTypes[0]; } } @@ -149,7 +149,7 @@ class _FcsShipmentEditorState extends State { labelText: AppTranslations.of(context)! .text('FCSshipment.shipment_type'), icon: Icon(Ionicons.ios_airplane, color: primaryColor)), - items: mainModel.setting.shipmentTypes + items: mainModel.setting!.shipmentTypes .map((e) => DropdownMenuItem(child: Text(e), value: e)) .toList(), onChanged: (String? selected) => { @@ -288,7 +288,7 @@ class _FcsShipmentEditorState extends State { _consigneeController.text != "" || _portController.text != "" || _destinationController.text != "" || - _currentShipmentType != mainModel.setting.shipmentTypes[0]; + _currentShipmentType != mainModel.setting!.shipmentTypes[0]; } else { FcsShipment fcsShipment = _getPayload(); return widget.shipment!.isChangedForEdit(fcsShipment); diff --git a/lib/pages/fcs_shipment/model/fcs_shipment_model.dart b/lib/pages/fcs_shipment/model/fcs_shipment_model.dart index aaf40a4..45d2141 100644 --- a/lib/pages/fcs_shipment/model/fcs_shipment_model.dart +++ b/lib/pages/fcs_shipment/model/fcs_shipment_model.dart @@ -11,13 +11,13 @@ import 'package:logging/logging.dart'; class FcsShipmentModel extends BaseModel { final log = Logger('FcsShipmentModel'); - StreamSubscription listener; + StreamSubscription? listener; List _fcsShipments = []; List get fcsShipments => _selectedIndex == 1 ? _fcsShipments : List.from(_shipped.values); - Paginator _shipped; + late Paginator _shipped; bool isLoading = false; int _selectedIndex = 1; set selectedIndex(int index) { @@ -25,7 +25,7 @@ class FcsShipmentModel extends BaseModel { notifyListeners(); } - get selectedIndex => _selectedIndex; + int get selectedIndex => _selectedIndex; @override void privilegeChanged() { @@ -43,12 +43,12 @@ class FcsShipmentModel extends BaseModel { } Future _loadFcsShipments() async { - if (user == null || !user.hasFcsShipments()) return; + if (user == null || !user!.hasFcsShipments()) throw "No Privilege"; String path = "/$fcs_shipment_collection/"; - if (listener != null) listener.cancel(); + if (listener != null) listener!.cancel(); _fcsShipments = []; try { - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection("$path") .where("status", isEqualTo: fcs_shipment_confirmed_status) .where("is_deleted", isEqualTo: false) @@ -56,9 +56,9 @@ class FcsShipmentModel extends BaseModel { .snapshots() .listen((QuerySnapshot snapshot) { _fcsShipments.clear(); - _fcsShipments = snapshot.documents.map((documentSnapshot) { + _fcsShipments = snapshot.docs.map((documentSnapshot) { var s = FcsShipment.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return s; }).toList(); notifyListeners(); @@ -69,9 +69,9 @@ class FcsShipmentModel extends BaseModel { } Paginator _getShipped() { - if (user == null || !user.hasFcsShipments()) return null; + if (user == null || !user!.hasFcsShipments()) throw "No Privilege"; - var pageQuery = Firestore.instance + var pageQuery = FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("status", isEqualTo: fcs_shipment_shipped_status) .where("is_deleted", isEqualTo: false) @@ -102,13 +102,13 @@ class FcsShipmentModel extends BaseModel { Future> getActiveFcsShipments() async { List fcsShipments = []; try { - var snaps = await Firestore.instance + var snaps = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("status", isEqualTo: fcs_shipment_confirmed_status) - .getDocuments(source: Source.server); - fcsShipments = snaps.documents.map((documentSnapshot) { + .get(const GetOptions(source: Source.server)); + fcsShipments = snaps.docs.map((documentSnapshot) { var fcs = FcsShipment.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return fcs; }).toList(); } catch (e) { @@ -117,13 +117,13 @@ class FcsShipmentModel extends BaseModel { return fcsShipments; } - Future getFcsShipment(String id) async { + Future getFcsShipment(String id) async { try { - var snap = await Firestore.instance + var snap = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") - .document(id) - .get(source: Source.server); - var fcs = FcsShipment.fromMap(snap.data, snap.documentID); + .doc(id) + .get(const GetOptions(source: Source.server)); + var fcs = FcsShipment.fromMap(snap.data as Map, snap.id); return fcs; } catch (e) { @@ -135,13 +135,13 @@ class FcsShipmentModel extends BaseModel { Future> getInvoiceFcsShipments() async { List fcsShipments = []; try { - var snaps = await Firestore.instance + var snaps = await FirebaseFirestore.instance .collection("/$fcs_shipment_collection") .where("pending_invoice_user_count", isGreaterThan: 0) - .getDocuments(source: Source.server); - fcsShipments = snaps.documents.map((documentSnapshot) { + .get(const GetOptions(source: Source.server)); + fcsShipments = snaps.docs.map((documentSnapshot) { var fcs = FcsShipment.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return fcs; }).toList(); } catch (e) { @@ -156,7 +156,7 @@ class FcsShipmentModel extends BaseModel { @override logout() async { - if (listener != null) await listener.cancel(); + if (listener != null) await listener!.cancel(); if (_shipped != null) _shipped.close(); _fcsShipments = []; } diff --git a/lib/pages/invoice/editor/invoice_discount_list.dart b/lib/pages/invoice/editor/invoice_discount_list.dart index 3381a3e..a86a056 100644 --- a/lib/pages/invoice/editor/invoice_discount_list.dart +++ b/lib/pages/invoice/editor/invoice_discount_list.dart @@ -6,7 +6,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; class InvoiceDiscountList extends StatelessWidget { - final List? discounts; + final List? discounts; const InvoiceDiscountList({ Key? key, @@ -68,14 +68,14 @@ class InvoiceDiscountList extends StatelessWidget { onSelectChanged: (value) => Navigator.pop(context, c), cells: [ MyDataCell(new Text( - c.code!, + c?.code ?? '', style: textStyle, )), MyDataCell( Row( mainAxisAlignment: MainAxisAlignment.end, children: [ - Text(c.amount.toStringAsFixed(2), style: textStyle), + Text(c?.amount.toStringAsFixed(2) ?? "", style: textStyle), ], ), ), diff --git a/lib/pages/invoice/editor/invoice_editor.dart b/lib/pages/invoice/editor/invoice_editor.dart index b40c834..c8b9efa 100644 --- a/lib/pages/invoice/editor/invoice_editor.dart +++ b/lib/pages/invoice/editor/invoice_editor.dart @@ -113,7 +113,7 @@ class _InvoiceEditorState extends State { }); } - List discounts = []; + List discounts = []; _loadDiscount() async { DiscountModel discountModel = Provider.of(context, listen: false); diff --git a/lib/pages/staff/model/staff_model.dart b/lib/pages/staff/model/staff_model.dart index cadb5a4..b2c2ebb 100644 --- a/lib/pages/staff/model/staff_model.dart +++ b/lib/pages/staff/model/staff_model.dart @@ -11,8 +11,8 @@ import 'package:logging/logging.dart'; class StaffModel extends BaseModel { final log = Logger('StaffModel'); - StreamSubscription listener; - StreamSubscription privilegeListener; + StreamSubscription? listener; + StreamSubscription? privilegeListener; List employees = []; List privileges = []; @@ -26,28 +26,28 @@ class StaffModel extends BaseModel { @override logout() async { - if (listener != null) listener.cancel(); - if (privilegeListener != null) privilegeListener.cancel(); + if (listener != null) listener!.cancel(); + if (privilegeListener != null) privilegeListener!.cancel(); employees = []; privileges = []; } Future _loadEmployees() async { - if (user == null || !user.hasStaffs()) return; + if (user == null || !user!.hasStaffs()) throw "No Privilege"; try { - if (listener != null) listener.cancel(); + if (listener != null) listener!.cancel(); - listener = Firestore.instance + listener = FirebaseFirestore.instance .collection("/$user_collection") .where("is_employee", isEqualTo: true) .where("is_sys_admin", isEqualTo: false) .snapshots() .listen((QuerySnapshot snapshot) { employees.clear(); - employees = snapshot.documents.map((documentSnapshot) { + employees = snapshot.docs.map((documentSnapshot) { var user = - User.fromMap(documentSnapshot.data, documentSnapshot.documentID); + User.fromMap(documentSnapshot.data as Map, documentSnapshot.id); return user; }).toList(); notifyListeners(); @@ -59,14 +59,14 @@ class StaffModel extends BaseModel { Future _loadPrivileges() async { try { - privilegeListener = Firestore.instance + privilegeListener = FirebaseFirestore.instance .collection("/$privilege_collection") .snapshots() .listen((QuerySnapshot snapshot) { privileges.clear(); - privileges = snapshot.documents.map((documentSnapshot) { + privileges = snapshot.docs.map((documentSnapshot) { var privilege = Privilege.fromMap( - documentSnapshot.data, documentSnapshot.documentID); + documentSnapshot.data as Map, documentSnapshot.id); return privilege; }).toList(); notifyListeners(); @@ -82,27 +82,27 @@ class StaffModel extends BaseModel { token: await getToken()); } - Future findUser(String phoneNumber) { + Future findUser(String phoneNumber) { return Services.instance.userService.findUser(phoneNumber); } Future> getPickupEmployees() async { - if (user == null || !user.hasShipment()) return []; + if (user == null || !user!.hasShipment()) return []; return _getUsers(privilege_shipment); } Future> _getUsers(String privilege) async { List users = []; try { - var snaps = await Firestore.instance + var snaps = await FirebaseFirestore.instance .collection("/$user_collection") .where("is_employee", isEqualTo: true) .where("is_sys_admin", isEqualTo: false) .where("privileges", arrayContains: privilege) - .getDocuments(source: Source.server); - users = snaps.documents.map((documentSnapshot) { + .get(const GetOptions(source: Source.server)); + users = snaps.docs.map((documentSnapshot) { var user = - User.fromMap(documentSnapshot.data, documentSnapshot.documentID); + User.fromMap(documentSnapshot.data as Map, documentSnapshot.id); return user; }).toList(); } catch (e) { diff --git a/lib/pages/staff/staff_editor.dart b/lib/pages/staff/staff_editor.dart index 5a703b0..67197e0 100644 --- a/lib/pages/staff/staff_editor.dart +++ b/lib/pages/staff/staff_editor.dart @@ -268,7 +268,7 @@ class _StaffEditorState extends State { _isLoading = true; }); try { - User _user = await staffModel.findUser(_phoneInput.text); + User? _user = await staffModel.findUser(_phoneInput.text); if (_user == null) { showMsgDialog(context, "Error", _phoneInput.text + " not found!"); return; diff --git a/lib/pages/widgets/local_popup_menu_button.dart b/lib/pages/widgets/local_popup_menu_button.dart index a73bb86..8fa1e62 100644 --- a/lib/pages/widgets/local_popup_menu_button.dart +++ b/lib/pages/widgets/local_popup_menu_button.dart @@ -78,7 +78,7 @@ class _LocalPopupMenuButtonState extends State { children: [ Icon( widget.buttonIcon ?? Icons.filter_list, - color: widget.buttonColor ?? primaryColor, + color: widget.buttonColor, ), hightlight ? Positioned( @@ -98,14 +98,12 @@ class _LocalPopupMenuButtonState extends State { )), itemBuilder: (BuildContext context) { return popmenus.map((LocalPopupMenu choice) { - if (choice == null) return null; return PopupMenuItem( value: choice, child: Row( children: [ - LocalText(context, choice.textKey, - color: - choice?.enabled ?? true ? primaryColor : Colors.grey), + LocalText(context, choice.textKey ?? "", + color: choice.enabled ? primaryColor : Colors.grey), SizedBox( width: 10, ), @@ -124,8 +122,8 @@ class _LocalPopupMenuButtonState extends State { bool _needHighlight() { popmenus.forEach((e) { - if (e == null) return false; - if (e.selected && e.highlight) return true; + if (e == null) return; + if (e.selected && e.highlight) return; }); return false; }