簡體   English   中英

如何在Dart中訪問子類的變量

[英]How to access variables of subclass in dart

我有一個名為Invoices的類,並且該類包含List<MenuInInvoice> menus類的List<MenuInInvoice> menus MenuInInvoice類具有兩個名為foodNameprice變量。

但是在main()類中,我無法訪問這些變量。

import 'dart:convert';

    import 'package:cloud_firestore/cloud_firestore.dart';

    Invoices invoicesFromJson(String str) => 
 Invoices.fromJson(json.decode(str));

    String invoicesToJson(Invoices data) => json.encode(data.toJson());

    class Invoices {
     String orderNo;
     String tableNo;
     String customerName;
     DateTime orderDate;
     List<MenuInInvoice> menus;
     DocumentReference reference;

     Invoices({
      this.orderNo,
      this.tableNo,
      this.customerName,
      this.orderDate,
      this.menus,
     });

     Invoices.fromJson(Map json,{this.reference}){
      orderNo = json["orderNo"] ?? "unknown";
      tableNo = json["tableNo"] ?? "unknown";
      customerName = json["customerName"] ?? "unknown";
      orderDate = DateTime.parse(json["orderDate"]);
      menus = new List<MenuInInvoice>.from(json["menus"].map((x) => MenuInInvoice.fromJson(x)));
     }

     Invoices.fromSnapshot(DocumentSnapshot snapshot):
        this.fromJson(snapshot.data, reference: snapshot.reference);


     Map<String, dynamic> toJson() => {
      "orderNo": orderNo ?? "unknown",
      "tableNo": tableNo ?? "unknown",
      "customerName": customerName ?? "unknown",
      "orderDate": "${orderDate.year.toString().padLeft(4, '0')}-${orderDate.month.toString().padLeft(2, '0')}-${orderDate.day.toString().padLeft(2, '0')}",
      "menus": new List<dynamic>.from(menus.map((x) => x.toJson())),
    };
  }

   class MenuInInvoice {
    String foodName;
    String price;

    MenuInInvoice({
     this.foodName,
     this.price,
    });

    factory MenuInInvoice.fromJson(Map json) => new MenuInInvoice(
     foodName: json["foodName"] ?? "unknown",
     price: json["price"] ?? "unknown",
    );

    Map<String, dynamic> toJson() => {
     "foodName": foodName ?? "unknown",
     "price": price ?? "unknown",
    };
  }

這是我的main()類:

Invoices invoices = new Invoices(
     tableNo: "01",
     orderNo: "001",
     customerName: "Jonh",
     orderDate: DateTime.now(),
     menus.foodName: "abc"
)

main()類中,我無法使用語句menus.foodName訪問類的變量。

我怎樣才能做到這一點? 提前致謝!

代替:

Invoices invoices = Invoices(
     tableNo: orderItem.tableNo,
     orderNo: orderItem.orderNo,
     customerName: orderItem.customerName,
     orderDate: DateTime.now(),
     menus: orderItem.menus.foodName
    )

嘗試:

Invoices invoices = new Invoices(
     tableNo: orderItem.tableNo,
     orderNo: orderItem.orderNo,
     customerName: orderItem.customerName,
     orderDate: DateTime.now(),
     menus: orderItem.menus.foodName
    )

我不確定您要達到什么目標。

Invoice構造器具有五個命名參數,其中一個稱為menus 調用構造函數時,您可以通過在參數前面寫一個參數名稱和一個冒號來傳遞命名參數的參數。

在您的主要代碼中:

Invoices invoices = new Invoices(
  tableNo: "01",
  orderNo: "001",
  customerName: "Jonh",
  orderDate: DateTime.now(),
  menus.foodName: "abc"
)

您可以正確地將參數傳遞給命名參數tableNoorderNocustomerNameorderDate

但是語法menus.foodname: "abc"無效。 沒有名為menus.foodName參數,它甚至不是有效的名稱(單個標識符)。

您能否描述您期望/想要執行的代碼,因為所提供的代碼尚不清楚。

實際上,您不能直接訪問foodName。

請嘗試以下方法:

List<MenuInInvoice> menuList = List<MenuInInvoice>();
menuList.add(MenuInInvoice(foodName: 'abc'));

Invoices invoices = Invoices(
    tableNo: '01',
    orderNo: '001',
    customerName: 'Jonh',
    orderDate: DateTime.now(),
    menus: menuList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM