Use nested maps to retrieve and save data to Firebase - Flutter

637
  1. My toMap and fromMap methods, are they rightly implemented?

    They look fine to me.

  2. Is this nested too deep?

    This is a small scale so I do not think you nested too much maybe when you are going to or if you are going to add more yes, then maybe it would be better to think of an better way to nest them.

  3. Should I leave Sales details and purchase items as maps or are they fine as their own classes?

    Really depends here on what you intend to do more with them if you want to reuse them in other parts you could try to make them as their own classes but if this is not needed they are good as they are.

  4. Is there a better way to current implementation? What's the recommended standard of working with models who depends on other class models?

    Looks fine and really depends on your needs on what you want to do. Since you said you want to write and read data to Cloud Firestore, I have checked the documentation (https://firebase.google.com/docs/firestore/query-data/get-data#java) for this and took the Java Android example and this should be the "recommended" way to do it.

Share:
637
hermie_brown
Author by

hermie_brown

Flutter Enthusiast! All things mobile and cool.

Updated on December 24, 2022

Comments

  • hermie_brown
    hermie_brown over 1 year

    I have three models I'm working with to retrieve and write data to my Cloud Firestore.

    //Sales model
    class Sales {
      String id;
      String userId;
      SalesDetails salesDetails;
      Timestamp createdAt;
      Timestamp updatedAt;
    
      Sales();
    
      Sales.fromMap(Map<String, dynamic> data) {
        id = data['id'];
        userId = data['user_id'];
        salesDetails = SalesDetails.fromMap(data['sales_details']);
        createdAt = data['created_at'];
        updatedAt = data['updated_at'];
      }
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'user_id': userId,
          'sales_details': salesDetails,
          'created_at': createdAt,
          'updated_at': updatedAt,
        };
      }
    }
    

    //SalesDetails model

    class SalesDetails {
      String id;
      PurchasedItems purchaseItems;
      String bankTransferSenderName;
      String chequeInfo;
      String creditPayLaterInfo;
      String customerId;
      String customerFirstName;
      String customerLastName;
      double subTotalAmount;
      double totalAmount;
      double taxesDue;
      bool isComplete;
      Timestamp createdAt;
      Timestamp updatedAt;
    
      SalesDetails();
    
      SalesDetails.fromMap(Map<String, dynamic> data) {
        id = data['id'];
        purchaseItems = PurchasedItems.fromMap(data['purchased_items']);
        bankTransferSenderName = data['bank_transfer_sender_name'];
        chequeInfo = data['cheque_info'];
        creditPayLaterInfo = data['credit_pay_later_info'];
        customerId = data['customer_id'];
        customerFirstName = data['customer_first_name'];
        customerLastName = data['customer_last_name'];
        subTotalAmount = data['sub_total_amount'];
        totalAmount = data['total_amount'];
        taxesDue = data['taxes_due'];
        isComplete = data['is_complete'];
        createdAt = data['created_at'];
        updatedAt = data['updated_at'];
      }
    
      Map<String, dynamic> toMap() {
        return {
          'id': id,
          'purchased_items': purchaseItems,
          'bank_transfer_sender_name': bankTransferSenderName,
          'cheque_info': chequeInfo,
          'credit_pay_later_info': creditPayLaterInfo,
          'customer_id': customerId,
          'customer_first_name': customerFirstName,
          'customer_last_name': customerLastName,
          'sub_total_amount': subTotalAmount,
          'total_amount': totalAmount,
          'taxes_due': taxesDue,
          'is_complete': isComplete,
          'created_at': createdAt,
          'updated_at': updatedAt,
        };
      }
    }
    

    //PurchasedItem model

    class PurchasedItems {
      String itemId;
      String itemName;
      String itemDescription;
      int itemQuantity;
    
      PurchasedItems();
        
      PurchasedItems.fromMap(Map<String, dynamic> data) {
        itemId = data['item_id'];
        itemName = data['item_name'];
        itemDescription = data['item_description'];
        itemQuantity = data['itemQuantity'];
      }
    
      Map<String, dynamic> toMap() {
        return {
          'id': itemId,
          'user_id': itemName,
          'item_quantity': itemQuantity,
          'item_description': itemDescription,
        };
      }
    }
    

    After watching the official Firestore videos on Maps vs Arrays vs Collection/documents, I've decided that sub collections / documents might just be the best for me due to how I am to display the information on my app.

    1. my toMap and fromMap methods, are they rightly implemented?
    2. Is this nested too deep?
    3. Should I leave Sales details and purchase items as maps or are they fine as their own classes?
    4. Is there a better way to my current implementation? What's the recommended standard of working with models who depends on other class models?

    Thanks.

  • hermie_brown
    hermie_brown over 3 years
    Thanks! I really appreciate.