Rodrigo
Rodrigo

Reputation: 305

Dart - Read Keys and Values from a Map inside a Map

I have a variable with the following structure:

final Map<String, Map<double, String>> ingredients;

I need to access, as string, the "inner" map keys and values after indexing the external map:

ingredients.keys.elementAt(index)

The above code returns a "regular string", but when accessing the "inner" map:

ingredients.values.elementAt(index).keys

The result prints with round brackets around it. I suppose it occurs because the first example returns a string and the second returns an Itarable. But how do I make it a string without the round brackets?

.toString() does not work.

I can't make it a single Map<String, String> because I need the double value separated from the second string (a unit specification).

I will put the result inside a Flutter Text() widget inside a ListView.builder(), that is the reason of the indexing.

Resuming: I am getting (200)(g) and I need 200g.

Thanks for the attention. Any help is appreciated.

Upvotes: 3

Views: 496

Answers (2)

mngkvn
mngkvn

Reputation: 116

Not a pretty solution but you can try: ingredients.values.elementAt(index).keys.toString().replaceAll(RegExp(r'[\(\)]'),'');

which will remove the parentheses anywhere in the keys.

I ran into the same problem as you did.

Hope this helps!

Upvotes: 0

Prabhanshu Tiwari
Prabhanshu Tiwari

Reputation: 54

try this -

var _value = ingredients.values.elementAt(index).values;
print(_value.substring(1,_value.length-1));

Upvotes: 1

Related Questions