Back

Attributes Confusion

  • 0
  • Databases
  • Flutter
  • General
  • Cloud
Arif Qayoom
15 Dec, 2024, 04:39
TypeScript

class OrdersModel {
  String id, email, name, phone, status, user_id, address;
  int discount, total, created_at;
  List<OrderProductModel> products;

  OrdersModel({
    required this.id,
    required this.created_at,
    required this.email,
    required this.name,
    required this.phone,
    required this.address,
    required this.status,
    required this.user_id,
    required this.discount,
    required this.total,
    required this.products,
  });

  // Convert Appwrite's Document to OrdersModel
  factory OrdersModel.fromJson(Document doc, [Strings]) {
    final data = doc.data;
    return OrdersModel(
      id: doc.$id,
      created_at: data['created_at'] ?? 0,
      email: data['email'] ?? '',
      name: data['name'] ?? '',
      phone: data['phone'] ?? '',
      status: data['status'] ?? '',
      address: data['address'] ?? '',
      user_id: data['user_id'] ?? '',
      discount: data['discount'] ?? 0,
      total: data['total'] ?? 0,
      products: List<OrderProductModel>.from(data['products']?.map((e) => OrderProductModel.fromJson(e)) ?? []),
    );
  }

  // Convert a list of Documents to a list of OrdersModels
  static List<OrdersModel> fromJsonList(List<Document> docs) {
    return docs.map((doc) => OrdersModel.fromJson(doc)).toList();
  }
}

class OrderProductModel {
  String id, name, image;
  int quantity, single_price, total_price;

  OrderProductModel({
    required this.id,
    required this.name,
    required this.image,
    required this.quantity,
    required this.single_price,
    required this.total_price,
  });

  factory OrderProductModel.fromJson(Map<String, dynamic> json) {
    return OrderProductModel(
      id: json['id'] ?? '',
      name: json['name'] ?? '',
      image: json['image'] ?? '',
      quantity: json['quantity'] ?? 0,
      single_price: json['single_price'] ?? 0,
      total_price: json['total_price'] ?? 0,
    );
  }
}```
TL;DR
Developers are confused about creating a product attribute. The code provided showcases how to convert a document to OrdersModel and a list of documents to a list of OrdersModels, including OrderProductModel.
Arif Qayoom
15 Dec, 2024, 04:40

How can I create thus Product Attribute can any one tell me please!

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more