Dart - Read Keys and Values from a Map inside a Map
try this -
var _value = ingredients.values.elementAt(index).values;
print(_value.substring(1,_value.length-1));
Rodrigo
Updated on December 20, 2022Comments
-
Rodrigo over 1 year
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.
-
jamesdlin almost 3 years"But how do I make it a string without the round brackets?" Can you provide an example of the exact output you're looking for? That is, given a
Map<double, String>
with values{1.0, 'some string'}
, what string representation do you want? -
Rodrigo almost 3 years@jamesdlin Sure. I am getting (200)(g) and I need 200g as string output.
-
jamesdlin almost 3 yearsCan the inner
Map
actually contain multiple key-value pairs? If not, it'd be simpler to define your own class (e.g.class Quantity { double amount; String units; Quantity(this.amount, this.units); String toString() => '$amount$units'; }
). -
Rodrigo almost 3 yearsI see, the inner map just haver a pair. I'll try to implement your Idea. Thank you very much.
-