在飞镖中创建自定义模型

问题描述 投票:1回答:2

我希望创建一个自定义模型,以后可以参考:该模型只是国家/地区及其指标的列表。创建模型后,我想像以下方式访问它:国家(“ au”)。人口]]

我已经创建了下面的类,但是除此之外,我不确定如何用数据初始化模型。我相信我需要一张Country实例的地图吗?

class Country {
  final String code;
  final String name;
  final int population;

  Country({
    this.code,
    this.name,
    this.population,
  });

  Map Countries = {'au': new Country('code': 'au', 'name': 'Australia', population: 25000000),
                   'uk': new Country('code': 'uk', 'name': 'United Kingdom', 'population': 66000000)}
}
    

我希望创建一个自定义模型,以后可以参考:该模型只是国家/地区及其指标的列表。创建模型后,我想像以下方式访问它:Countrys('au')....

dart
2个回答
0
投票

您应该使用工厂构造函数来初始化您的类,并将所有模型都置于静态方法中。


0
投票

使用数据初始化模型:

© www.soinside.com 2019 - 2024. All rights reserved.