繁体   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