'Extracting values from keys inside multiple key-value pair arrays with dart/flutter
Below I have a simple array that contains key-value pairs. Here's what I'm trying to achieve:
If 'global' == true, print the value of the 'title' key inside that same object. If 'global' == false, do nothing.
So for the example below, we would only print 'hello'.
Any help is greatly appreciated!
void main() {
var messages = [
{
'id': 1,
'title': "hello",
'global': true,
},
{
'id': 2,
'title': "bye",
'global': false,
},
];
Solution 1:[1]
You could simply iterate into messages:
var messages = [
{
'id': 1,
'title': "hello",
'global': true,
},
{
'id': 2,
'title': "bye",
'global': false,
},
{
'id': 3,
'title': "hi",
'global': true,
},
];
messages.forEach((element) {
element["global"] == true ? print(element["title"]) : null;
});
Output:
hello
hi
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani3le_ |
