List of tuples in flutter/dart

13,158

Solution 1

Example of how you can iterate over your data pairs by using a Map as data structure:

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:

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.

Solution 2

If you want to use package:tuple, you could do:

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})');
  }
}
Share:
13,158

Related videos on Youtube

Uwe.Schneider
Author by

Uwe.Schneider

Updated on June 04, 2022

Comments

  • Uwe.Schneider
    Uwe.Schneider 6 months

    I would like to have a list of tuples in dart/flutter, loop over them and read the first and second variable. There is a pub.dev package that creates a tuple in flutter. Is there another way in dart? Do I need to use a class to create a tuple like the flutter package does?

    I began to create an example in 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];
      }
    }
    
    • happy_san
      happy_san about 2 years
      You cannot install/use pub.dev packages in dartpad. Try them in your local sdk installation.
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      The question was more generally, wether one can do it without this package. And how it would look in flutter. This is not a question about dartpad
    • Christopher Moore
      Christopher Moore about 2 years
      Why does it have to be a "tuple"? What's wrong with a List?
    • happy_san
      happy_san about 2 years
      Unfortunately no. You'll have to use this package. See this for more insight stackoverflow.com/questions/45326310/…
    • Christopher Moore
      Christopher Moore about 2 years
      @happy_san I don't see why it's a requirement to use this package. What's the issue with a List as your linked question suggests?
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @Christopher Moore It does not have to be tuple class and it should not be. The testList at is it defined above is ill-defined and I would like to know the the correct form if existent.
    • Christopher Moore
      Christopher Moore about 2 years
      @Uwe.Schneider What do you mean "correct form"? Again, what's wrong with just using a List?
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @happy_san Thanks for the answer, but the question in your post is slightly different. Maybe it is impossible to return multiple values but possible to have a list with each item being a tuple.
    • Christopher Moore
      Christopher Moore about 2 years
      @Uwe.Schneider Can you address my previous comments?
    • happy_san
      happy_san about 2 years
      @Uwe.Schneider Could you explain your use case?
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @ Christopher Moore Sure, sorry for the delay. I need to loop over a list of two values. Either I take two lists and zip them together. Or I create one list of tuple objects, or list objects of two elements. I would prefer the second option with tuples because it could easily take Python outputs.
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @ happy_san I would like to index a list of tuples where the first entry is a path to an image (String) and the second is the class it belongs to (Int).
    • julemand101
      julemand101 about 2 years
      Considered using a Map for this data structure?
    • Christopher Moore
      Christopher Moore about 2 years
      @Uwe.Schneider What's wrong with a List? There is no "correct form", it's just whatever works for you.
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @ julemand101 Yes, but I assume, the problem with a tuple stays the same. Actually, I just need to understand how to create a map/list element, whose entries are either tuples or lists of two values, (or another map with two entries.)
    • Uwe.Schneider
      Uwe.Schneider about 2 years
      @ Christopher Moore There is no problem with a list, but the current way of the testList - in the Python form - throws errors. So I would like to have the "corrected form" of my attempt in the example.
    • Abion47
      Abion47 about 2 years
      There is no such thing as a tuple in native Dart. Either use the external tuple package or use another data structure like List or Map. That's pretty much the long and short of it.
    • jamesdlin
      jamesdlin about 2 years
      @ChristopherMoore A List is a collection of elements with a homogeneous type (which would end up being Object if the elements aren't the same type). A tuple is a collection of elements with heterogeneous types. A Map could be a substitute if there are only two elements.
  • wu-lee
    wu-lee 12 months
    There's already MapEntry, which is essentially the same as Pair above.
  • julemand101
    julemand101 12 months
    @wu-lee Yes, but just because we have a class which have a similar behavior, it is not a reason to use it for an purpose it is not intended for (the documentation for MapEntry says: "A key/value pair representing an entry in a Map."). It would be very weird to use MapEntry around your code every time you want to have a method returning two values. But should be noted I am already using MapEntry in my first example implicit since that is what we get from testMap.entries. But that does also makes sense since I am using a Map here.