首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据条件从Dart中的对象列表中返回一个值

根据条件从Dart中的对象列表中返回一个值
EN

Stack Overflow用户
提问于 2020-09-07 07:29:40
回答 2查看 1.5K关注 0票数 0

我有一个国家对象的列表: countryName,countryCode,currencyName,currencyCode,countryCapitalCity,countryLatitude,countryLongitude。这些对象的存储方式如下:

代码语言:javascript
复制
    Country('Ghana', '+233', 'Ghanaian Cedi', 'GHS', 'Accra', 5.603717,
        -0.186964, 22),
    Country('Guinea', '+224', 'Guinean Franc', 'GNF', 'Conakry', 9.641185,
        -13.578401, 23),
    Country('Guinea-Bissau', '+245', 'West African CFA Franc', 'XOF', 'Bissau',
        11.881655, -15.617794, 24),
    Country('Ivory Coast', '+225', 'West African CFA Franc', 'XOF',
        'Yamoussoukro', 6.827623, -5.289343, 25),

我实例化了该列表如下:

代码语言:javascript
复制
    List<Country> _countries = Country.countries;

我有一个变量: userCountryCode,它将被提供-例如,以+245为例。我想查看_countries的列表,并返回值11.881655、-15.617794,这些值映射到对象的countryLatitude,countryLongitude属性。

下列措施未起作用:

代码语言:javascript
复制
    List<Country> dumy =
       _countries.where((element) => element.countryCode == result).toList();
EN

回答 2

Stack Overflow用户

发布于 2020-09-07 07:42:24

另一种方法是使用Map。在您的例子中,您可以创建Map<String, List<Country>>作为countryCode的键(我理解这并不是唯一的,因为您可以检索多个Country)。

然后,您只需要搜索像mapName[countryCodeKey]这样的键,它将返回一个List<Country>,该List<Country>将是所有拥有该密钥的控件。

这比遍历所有列表项的速度要快得多。如果您知道Big表示法,则您的解是O(n),而使用映射是O(1)。您只需在列表中迭代一次就可以创建映射。

要创建地图,可以使用:

代码语言:javascript
复制
Map<String, Country> = {
  for (Country c in _countries)
    c.countryCode: c
};

一个例子是:

代码语言:javascript
复制
void main() {
  List<Country> _countries = [
    Country(
      'Ghana',
      '+233',
      'Ghanaian Cedi',
      'GHS',
      'Accra',
      5.603717,
      -0.186964,
    ),
    Country(
      'Guinea',
      '+224',
      'Guinean Franc',
      'GNF',
      'Conakry',
      9.641185,
      -13.578401,
    ),
    Country(
      'Guinea-Bissau',
      '+245',
      'West African CFA Franc',
      'XOF',
      'Bissau',
      11.881655,
      -15.617794,
    ),
    Country(
      'Ivory Coast',
      '+225',
      'West African CFA Franc',
      'XOF',
      'Yamoussoukro',
      6.827623,
      -5.289343,
    ),
    Country(
      'Another Country',
      '+225',
      'Atlantida',
      'BTC',
      'Bitcoin',
      12.827623,
      -155.289343,
    ),
  ];

  Map<String, List<Country>> countryMap = {};
  for (Country country in _countries) {
    countryMap.putIfAbsent(country.countryCode, () => []).add(country);
  }

  // Let's say you are looking for the +233 code
  countryMap['+233'].forEach((item) {
    print(item.countryLongitude);
    print(item.countryLatitude);
  });

  // If you want it null-aware
  countryMap['+234']?.forEach((item) {
    print(item.countryLongitude);
    print(item.countryLatitude);
  });
  // By using ?. the property or method will only be called if
  // the item is not null

  // If you want to check if a code exists
  if (countryMap.containsKey('+312')) {
    print('Existing code!');
  } else {
    print('This code does not exist!');
  }

  // If you want to keep the list of contries with a code
  List<Country> countriesWithId = countryMap['+225'];
  print(countriesWithId.length);
  // Prints 2 since there are two items with the id of +255
}
票数 1
EN

Stack Overflow用户

发布于 2020-09-07 07:29:40

当我提出这个问题时,我想出了答案,而不是放弃解决方案,我认为分享是明智的--我/我们将来可能会用到它:

代码语言:javascript
复制
    List<Country> dumy =
        _countries.where((element) => element.countryCode == result).toList();
     for (var d in dumy) {
      var clat = d.countryLatitude;
      var clong = d.countryLongitude;
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63773104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档