4

I tried some other solutions to this like using asMap() and forEach but kept getting different errors. Like it was saying the return type of my ChartBar isn't a 'MapEntry', as defined by anonymous closure, or The expression here has a type of 'void', and therefore cannot be used.

Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    crossAxisAlignment: CrossAxisAlignment.end,
    children: myList.map((data) {
      return ChartBar(
          ///etc
    }).toList(),
  )

I want the index as well.

11
  • @mirkancal That's the question I was referring to, the solutions gave me the errors I described above. Commented Aug 23, 2019 at 15:45
  • Can you try myList.asMap().map((MapEntry data) => data.value).toList() ? Commented Aug 23, 2019 at 15:47
  • @mirkancal As described in the question, I get the error ChartBar isn't a 'MapEntry' Commented Aug 23, 2019 at 15:49
  • What is ChartBar? A widget of your own? Commented Aug 23, 2019 at 15:58
  • @mirkancal Yeah it's my own widget. Commented Aug 23, 2019 at 16:00

2 Answers 2

9

mirkancal's suggestion didn't work because Map.map returns another Map (and therefore the enumeration callback you pass it is expected to return a MapEntry).

You instead need to use Map.entries so that you can use Iterable.map instead to construct a List:

Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    crossAxisAlignment: CrossAxisAlignment.end,
    children: myList.asMap().entries.map((MapEntry entry) {
      return ChartBar(entry.key, entry.value);
    }),
  )

You alternatively can use Dart's new collection-for construct:

Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    crossAxisAlignment: CrossAxisAlignment.end,
    children: [for (MapEntry entry in myList.asMap().entries)
      ChartBar(entry.key, entry.value)
    ],
  )

In the above, entry.key will be the index, and entry.value will be the original value in myList.

Sign up to request clarification or add additional context in comments.

8 Comments

With this I get the error The type 'Map<int, String>' used in the 'for' loop must implement Iterable. or The type 'Map<int, double>' used in the 'for' loop must implement Iterable. with a different List.
@Hasen Oops, I forgot to add .entries. I've updated my answer.
It's kind of odd code to me, why is it all contained in square brackets?
@Hasen I've updated my answer with a better explanation.
I guess it's the equivalent of the toList() with a map, you're constructing a List inside the square brackets.
|
-1

For the index you could use indexOf(value) function from the list to get index value. For example:

List list = List.generate(3, (int index) => index * index); // [0, 1, 4]

list.indexOf(4) // returns 2

As for your other issue, I'd need to see more code to see how you populate myList. I would suggest this approach (if it fits) to render your Row children. Use a function to return that list, for example:

buildList() {
 List<Widget> list = List<Widget>();

 chartBars.forEach((chart) => { //chartBars list with info
  // add your ChartBar widget to the list with appropiate info
  list.add(ChartBar(someProperty: chart.property));
 });
 return list;
}

Then on your Row widget just call that function when you build it's children:

Row(
    mainAxisSize: MainAxisSize.min,
    mainAxisAlignment: MainAxisAlignment.spaceAround,
    crossAxisAlignment: CrossAxisAlignment.end,
    children: buildList()      
)

6 Comments

Splitting widgets to methods is an antipattern though. iirokrankka.com/2018/12/11/…
Is there no simpler way to do it? There's no simple tweak to my existing code just to get the index to display?
@mirkancal thanks for the heads up, I'm relatively new to Flutter and may not be aware of best practices, @Hasen Have you tried using myList.indexOf(data) to show the index?
Using indexOf() would be rather inefficient, making what should be an O(n) operation instead O(n^2).
@mirkancal Yes indexOf() works but like it doesn't seem like the most eloquent way to do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.