I have something that looks like this:
const Books = <Map<String, dynamic>>[
{'name': 'The book 1', 'price': 30},
{'name': 'The book 2', 'price': 50},
];
const Newspapper = <Map<String, dynamic>>[
{'name': 'The Newspapper 1', 'price': 4},
{'name': 'The Newspapper 2', 'price': 9},
];
What's the best way so I can call the name of the variable? It's even possible to print the variable name? For example, print('you are reading $booksvariable') // you are reading books
also tried with something simmilar
const allList = <Map<String, dynamic>>[
{
"books": [
{'name': 'The book 1', 'price': 30},
{'name': 'The book 2', 'price': 50},
]
},
{
"newspapper": [
{'name': 'The Newspapper 1', 'price': 4},
{'name': 'The Newspapper 2', 'price': 9},
]
}
];
But still can't manage to disnplay only the "books" and "newspapper".
CodePudding user response:
print(books); //you are reading [{name: The book 1, price: 30}, {name: The book 2, price: 50}]
print(books[1]); //you are reading {name: The book 2, price: 50}
print(allList); //you are reading [{books: [{name: The book 1, price: 30}, {name: The book 2, price: 50}]}, {newspapper: [{name: The Newspapper 1, price: 4}, {name: The Newspapper 2, price: 9}]}]
books.map((e) => print('you are reading ${e['name']}')).toList(); //you are reading The book 1 \n you are reading The book 2
allList.elementAt(0).map((key, value) {
print(value); // [{books: [{name: The book 1, price: 30}, {name: The book 2, price: 50}]}, {newspapper: [{name: The Newspapper 1, price: 4}, {name: The Newspapper 2, price: 9}]}]
return MapEntry(key, value);
});
books.forEach((element) {
print('you are reading ${element['name']}'); //you are reading The book 1 \n you are reading The book 1
});
CodePudding user response:
If your goal is to combine the two Lists
and just print out the name this will suffice:
const Books = <Map<String, dynamic>>[
{'name': 'The book 1', 'price': 30},
{'name': 'The book 2', 'price': 50},
];
const Newspapper = <Map<String, dynamic>>[
{'name': 'The Newspapper 1', 'price': 4},
{'name': 'The Newspapper 2', 'price': 9},
];
// Combine the two lists
var combinedList = [];
combinedList.addAll(Books);
combinedList.addAll(Newspapper);
// Get the names by mapping them and print them
var names = combinedList.map((b) => b['name']);
names.forEach((name) {
print("You are reading $name");
});
Output:
I/flutter (20249): You are reading The book 1
I/flutter (20249): You are reading The book 2
I/flutter (20249): You are reading The Newspapper 1
I/flutter (20249): You are reading The Newspapper 2
If you want to print those two seperately you can do this:
const Books = <Map<String, dynamic>>[
{'name': 'The book 1', 'price': 30},
{'name': 'The book 2', 'price': 50},
];
const Newspapper = <Map<String, dynamic>>[
{'name': 'The Newspapper 1', 'price': 4},
{'name': 'The Newspapper 2', 'price': 9},
];
// Get the names and print them
Books.forEach((book) {
print("You are reading the book ${book['name']}");
});
Newspapper.forEach((newspapper) {
print("You are reading the newpaper ${newspapper['name']}");
});
Output:
I/flutter (20249): You are reading the book The book 1
I/flutter (20249): You are reading the book The book 2
I/flutter (20249): You are reading the newpaper The Newspapper 1
I/flutter (20249): You are reading the newpaper The Newspapper 2
It is up to you if you want to map
the name first (like in the first example) or just access the name directly in the loop (like ${book['name']}
in the second example). Hope I could help.