Dart,嵌套类,如何访问子类变量

时间:2019-03-19 14:17:15

标签: dart flutter

我是Dart / Flutter的新手。见谅我正在尝试创建一个在下面称为TestData的Object类。 TestData中的变量之一是TestChildClass的Map。如何访问子变量并进行设置。并得到他们。

    class TestData{
  int id;
var childClass = new Map<TestChildClass, String>();
TestData.items({
    this.id,
  this.childClass

});
}

class TestChildClass{
  int childid;

}

List <TestData> data = [
  TestData.items(
    id: 1,

    //childClass: {TestChildClass.:1, 1} how do i set and get this 
  )
];

还要对此进行跟进。

如何遍历Map并迭代字符串中的值。我想要一个简单的childClass.getData函数。会通过childClass并将所有Key值转换为字符串。

谢谢!

3 个答案:

答案 0 :(得分:3)

class APIConstant {
  static RequestKeys requestKeys = const RequestKeys();
  static ResponseKeys responseKeys = const ResponseKeys();

  static const String baseUrl = 'Your Project base url';
}

class RequestKeys {
  const RequestKeys();
  String get email => 'email';
  String get password => 'password';
}

class ResponseKeys {
  const ResponseKeys();
  String get data => 'data';
  String get status => 'status';
}

您可以这样使用:

print(APIConstant.requestKeys.email);
print(APIConstant.requestKeys.email);
print(APIConstant.baseUrl);

答案 1 :(得分:0)

只需在()类之后添加TestChildClass,即可在Map中定义List

class TestData{
  int id;
  var childClass = new Map<TestChildClass, dynamic>();
  TestData.items({
    this.id,
    this.childClass

  });
}

class TestChildClass{
  int childid;

}

List <TestData> data = [
  TestData.items(
    id: 1,
    childClass: {TestChildClass()..childid=5:"anything"},
  )
];

答案 2 :(得分:0)

You can do this way (add constructor to TestChildClass)

class TestData{
  int id;
  var childClass = new Map<TestChildClass, dynamic>();
  TestData.items({
    this.id,
  this.childClass

  });
}

class TestChildClass{
  TestChildClass(this.childid);
  int childid;

}

List <TestData> data = [
  TestData.items(
    id: 1,

    childClass: {TestChildClass(1): 1}
  )
];