This is the current state of the Map data structure that I am working with in Dart:
Map database = {
'campusConnect': {
'hashtags': [
{
'id': 4908504398,
'title': '#NeverGiveUp!',
'author': 'ABC',
},
{
'id': 430805,
'title': '#ItAlwaysTakesTime',
'author': 'XYZ'
}
]
}
};
I want to go over the hashtags array. Iterating over each object in that array, I want to compare the 'id' field with some number that I already have. How do I do it?
So far, this is what I have tried doing:
database['campusConnect']['hashtags'].map( (item) {
print('I am here ...');
if (item['id'] == hashtagId) {
print(item['title']);
}
});
I have no idea why, but it gives me no error and does not work at the same time. When I run it, it does not even print "I am here ...".
Notice the identifier "hashtagId" in the if block:
if (item['id'] == hashtagId) { ... }
This is being passed in the function as an argument. For simplicity, I have not show the function, but assume that this is an int-type parameter I am receiving
How should I accomplish this?
Dart is smart enough to skip the code that does nothing. In your case, you are not using the result of map()
function. Try to change it with forEach()
and fix hashTags
typo
database['campusConnect']['hashTags'].forEach((item) {
print('I am here ...');
if (item['id'] == hashtagId) {
print(item['title']);
}
});