简体   繁体   English

flutter/dart 中的元组列表

[英]List of tuples in flutter/dart

I would like to have a list of tuples in dart/flutter, loop over them and read the first and second variable.我想在 dart/flutter 中有一个元组列表,遍历它们并读取第一个和第二个变量。 There is a pub.dev package that creates a tuple in flutter. Is there another way in dart?有一个pub.dev package在flutter中创建一个元组。dart中还有其他方法吗? Do I need to use a class to create a tuple like the flutter package does?我是否需要使用 class 来创建像 flutter package 那样的元组?

I began to create an example in dart pad我开始在dart pad中创建一个例子

void main() {
  List testList = [("a",1),("b",2),("c",3),("d",4),("e",5)];
  
  
  for (var i = 0; i < 5; i++) {
    print('hello $i');
    currentItem = testList[i];

  }
}

Example of how you can iterate over your data pairs by using a Map as data structure:如何使用Map作为数据结构迭代数据对的示例:

void main() {
  final testMap = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5};

  for (final mapEntry in testMap.entries) {
    final key = mapEntry.key;
    final value = mapEntry.value;

    print('Key: $key, Value: $value');
    // Key: a, Value: 1
    // Key: b, Value: 2
    // Key: c, Value: 3
    // Key: d, Value: 4
    // Key: e, Value: 5
  }
}

You can also rather easy introduce your own Pair class which allows you to bind two objects together:你也可以很容易地引入你自己的Pair类,它允许你将两个对象绑定在一起:

class Pair<T1, T2> {
  final T1 a;
  final T2 b;

  Pair(this.a, this.b);
}

void main() {
  final testList = [
    Pair("a", 1),
    Pair("b", 2),
    Pair("c", 3),
    Pair("d", 4),
    Pair("e", 5)
  ];

  for (final pair in testList) {
    print('${pair.a} -> ${pair.b}');
  }
  // a -> 1
  // b -> 2
  // c -> 3
  // d -> 4
  // e -> 5
}

In most cases I will recommend you to make a class for you specific need since it makes it easier to make type safe code.在大多数情况下,我会建议您为您的特定需求创建一个class ,因为它可以更轻松地编写类型安全的代码。

If you want to use package:tuple , you could do:如果你想使用package:tuple ,你可以这样做:

void main() {
  var testList = <Tuple2<String, int>>[
    Tuple2("a", 1),
    Tuple2("b", 2),
    Tuple2("c", 3),
    Tuple2("d", 4),
    Tuple2("e", 5),
  ];
  
  for (var tuple in testList) {
    print('current tuple: (${tuple.item1}, ${tuple,item2})');
  }
}

Dart 3, (which is now, january 2023 in alpha), includes a new feature called Records . Dart 3,(现在是 2023 年 1 月的 alpha 版本)包括一个名为Records的新功能。

In most languages, a tuple has usually only positional fields, and a record has named fields.在大多数语言中,元组通常只有位置字段,而记录有命名字段。 Dart combines these two. Dart 结合了这两者。 A Record in dart can have both positional and named fields. dart 中的Record可以同时具有位置字段和命名字段。

When you create a Record with only positional fields, it's just a tuple:当您创建仅包含位置字段的Record时,它只是一个元组:

var tuple = ("first", 2, true);

This is the specification:这是规范:

https://github.com/dart-lang/language/blob/master/accepted/future-releases/records/records-feature-specification.md https://github.com/dart-lang/language/blob/master/accepted/future-releases/records/records-feature-specification.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM