I try to map a return of a map but it look like the first map dont return the result of the iteration. I'm kind of confuse here.
I try that code. It simply iterate the "row" and return the data of the iteration:
void main() {
final Map<String, dynamic> treeData = {
"title": "test",
"row": [
[
{
"img":
"https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg"
},
{
"img":
"https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg"
}
],
[
{
"img":
"https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg"
}
]
],
};
var mappedData = treeData['row'].map((data) {
print("");
print("!!!!!!!!!!!!!!!!!!!");
print(data);
return data;
}).toList().map((data) {
print("");
print("-------------------");
print(data);
return data;
}).toList();
print("");
print("???????????");
print(mappedData);
}
The result that I got isnt what I expected. It looks like the 2 iterations is exactly the same thing, as you can see.
!!!!!!!!!!!!!!!!!!!
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}, {img: https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg}]
!!!!!!!!!!!!!!!!!!!
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}]
-------------------
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}, {img: https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg}]
-------------------
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}]
???????????
[[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}, {img: https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg}], [{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}]]
I would expect something like that, with the second iteration that iterate throught the second array and return the object value.
!!!!!!!!!!!!!!!!!!!
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}, {img: https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg}]
!!!!!!!!!!!!!!!!!!!
[{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}]
-------------------
{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}
{img: https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg}
-------------------
{img: https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg}
There is a way to make it works in that way? What would be the best alternative?
Thanks for your answers.
CodePudding user response:
Yes, there's a way to make it work like that:
void main() {
final Map<String, dynamic> treeData = {
"title": "test",
"row": [
[
{
"img":
"https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg"
},
{
"img":
"https://cdn.pixabay.com/photo/2015/03/27/13/16/maine-coon-694730_960_720.jpg"
}
],
[
{
"img":
"https://cdn.pixabay.com/photo/2014/11/30/14/11/cat-551554_960_720.jpg"
}
]
],
};
var mappedData = treeData['row']
.map((data) {
print("");
print("!!!!!!!!!!!!!!!!!!!");
print(data);
return data;
})
.toList()
.map((data) {
for (var item in data) {
print("");
print(item);
}
print("-------------------");
return data;
})
.toList();
print("");
print("???????????");
print(mappedData);
}
Essentially you want to print every item of the list you are passing to that second function. You are getting the same result of the second iteration because you are transforming the output of the first iteration into the same input that function is receiving.
CodePudding user response:
Oh, I feel like it was late in the night and I was tired when I ask the question.. I find my solution to my misleading question..
Row(children: [
...treeData['row']
.map((data) => Column(children: [
...data.map((item) => Image.network(
item['img'],
fit: BoxFit.cover,
alignment: Alignment.topCenter,
))
]))
.toList()
That was so simple and easy.. It seems that sometime we need to sleep guys(me) .. ><
