How to perform a search in array of object flutter? - list

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}].
I already tried containsValue but I don't want to use it.
The result i need to get is, when i search for m i need to get [{"name": "mike", "age": "20"}, {"name":"william","age": "23"}] , and when i search 3 i need the result as [{"name":"william","age": "23"}].

You could create a Person or User class as julemand101 has suggested but if You have to work with Map try this:
List<Map<String, dynamic>> search(String input){
return my_List.where((e) => e["name"].contains(input) || e["age"].contains(input)).toList();
}

Related

How to perform search for a Map in flutter?

I am having a Map list with key and values, for example :
map<String, dynamic> my_List = [{"name": "mike", "age": "20"}, {"name": "william", "age": "23"}].
I tried containsValue, but I don't want to use it.
The result I want to get is when search for "i" then I need to get the result like {"mike" and "william"} and when I search for "2" I need only the result {20 and 23}.
Try something like:
String search = "k";
var matchNames = my_List.where(p => p["name"].contains(search)).map(p => p["name"]);
String result = "";
foreach(var name in matchNames){
result += name;
if(name != matchNames.last){
result += " and ";
}
}

List<Map<String, Object>> to Map<String, String[]>

I have a List of Map List<Map<String, Object>>. I want to move to Map<String, String[]>
Can someone please let me know how to convert?
List<Map<String, Object>> currentList = new ArrayList<Map<String, Object>>();
Map<String, Object> currMap = new HashMap<String, Object>();
currMap.put("A", "ABC");
currMap.put("B", "PQR");
currMap.put("C", "XYZ");
currentList.add(currMap);
currMap.put("A", "123");
currMap.put("B", "456");
currMap.put("C", "789");
currentList.add(currMap);
currMap.put("A", "OOO");
currMap.put("B", "ZZZ");
currentList.add(currMap);
To-be :
"A", ["ABC", "123", "OOO"],
"B", ["PQR", "456", "ZZZ"],
"C", ["XYZ", "789", ""]
One way using streams would be:
currentList.stream()
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(Entry::getValue, Collectors.toList())));
This would result in:
{A=[ABC, 123, OOO], B=[PQR, 456, ZZZ], C=[XYZ, 789]}
As a sidenote, the way you are generating currentList in the example does not result in a list of maps as you are probably expecting. As it is, you will end up with 3 references to the same map in the list.

How can I convert json map to List String?

I need to get country name and country code into List as "Andorra (AD)". I can load json to a map but I cannot convert to List. How can I convert json map to List String?
"country": [
{
"countryCode": "AD",
"countryName": "Andorra",
"currencyCode": "EUR",
"isoNumeric": "020"
},
You can use the .map() function
var countryList = country.map((c) => '${c["countryName"]} (${c["countryCode"]})').toList()

How to change json object name without changing its values in C++?

I'm using json for modern c++.
And I have a json file which contains some data like:
{
"London": {
"Adress": "londonas iela 123",
"Name": "London",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
And I found out a way to modify the values of "Adrese", "Name", "Shortname".
As you can see I have "name" and key element name set to the same thing.
But I need to change both the key element and value "name".
So at the end when somehow in the code I modify it, it would look like:
{
"Something_New": {
"Adress": "londonas iela 123",
"Name": "Something_New",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
I've tried:
/other_code/
json j
/functions_for_opening_json file/
j["London"]["Name"] = "Something_New"; //this changes the value "name"
j["London"] = "Something_New"; //But this replaces "London" with
"Something_new" and deletes all of its inside values.
Then I tried something like:
for(auto& el : j.items()){
if(el.key() == "London"){
el.key() = "Something_New";}
}
But that didn't work either.
I would like something like j["London"] = "Something_new", and for it to keep all the values that originally was for "London".
The value associated with key "London" is the entire subtree json object containing the other 3 keys with their values. This line j["London"] = "Something_New"; does not change the key, "London" but its value. So you end up with the pair "London" : "Something new", overwriting the json subtree object. The keys are stored internally as std::map . Therefore you can't simply rename a key like that. Try:
void change_key(json &j, const std::string& oldKey, const std::string& newKey)
{
auto itr = j.find(oldKey); // try catch this, handle case when key is not found
std::swap(j[newKey], itr.value());
object.erase(itr);
}
And then
change_key(j, "London", "Something_New");

Lists AS value of a Map in Dart

I want to create a map of members, but every membres have 3 propreties : first name, last name, and username. How can I create like a list of liste, but with a map.
So I want to have something like :
var membres= {['lastname': 'Bonneau',
'firstname': 'Pierre',
'username': 'mariobross'],
['lastname': 'Hamel',
'firstname': 'Alex',
'username': 'Queenlatifa'],
};
As you know, this code doesn't work. But it explain pretty well what I am trying to do.
I think you are confusing the two constructs here.
Read this introduction to the language: http://www.dartlang.org/docs/dart-up-and-running/ch02.html#lists
A list is a list of elements which can be denoted with the shorthand [...] syntax:
var list = [1, 2, "foo", 3, new Date.now(), 4];
Whereas a map can be denoted with the curly brace shorthand syntax:
var gifts = { // A map literal
// Keys Values
'first' : 'partridge',
'second' : 'turtledoves',
'fifth' : 'golden rings'
};
So, let's modify your code to work:
var members = [
{
'lastname': 'Bonneau',
'firstname': 'Pierre',
'username': 'mariobross'
},
{
'lastname': 'Hamel',
'firstname': 'Alex',
'username': 'Queenlatifa'
}
];
You can, for example, print the information like this:
members.forEach((e) {
print(e['firstname']);
});
If I understand your intent correctly, you want to have a list of maps. What you have is correct except you confused [ and {. The following works:
var membres = [
{'lastname': 'Bonneau',
'firstname': 'Pierre',
'username': 'mariobross'},
{'lastname': 'Hamel',
'firstname': 'Alex',
'username': 'Queenlatifa'}
];
As an example, to get a list of all usernames:
print(membres.map((v) => v['username']));
If you don't really need a Map, what about using a class to improve the structure of your code :
class Member {
String firstname;
String lastname;
String username;
Member(this.firstname, this.lastname, this.username);
}
main() {
final members = new List<Member>();
members.add(new Member('Pierre', 'Bonneau', 'mariobross'));
members.add(new Member('Alex', 'Hamel', 'Queenlatifa'));
// use members
}
You mean like this?
// FirstName => LastName => Value
var lookup = new Map<String, Map<String, String>>();
// get / set values like this
void setValue(String firstName, String lastName, String value) {
if (!lookUp.containsKey(firstName))
lookUp[firstName] = new Map<String, String>();
lookUp[firstName][lastName] = value;
}
String getValue(String firstName, String lastName) {
if (!lookUp.containsKey(firstName)) return "";
return lookUp[firstName][lastName];
}
First of all you need to create a map with value as list. Dont forget to initialize it
then if you want to fill it you first need to use built in function like putIfAbsent as in dart to add first object in list and then use update to add items in list. therefore you will need two arrays. First to put elements and then to add elements in list with same key. Also you can use try catch to identify if the key is present or not to do that in one loop
for (var item in days) {
var date_time = DateTime.parse(item["date"] + " 00:00:00");
_events[date_time] = _events.putIfAbsent(
date_time,
() => [
{
"title": item["title"],
"date": item["date"],
"time": reUse.get_time_am_pm_format(item["time"]),
"feature": item["feature"],
}
]);
}
for (var item in days) {
var date_time = DateTime.parse(item["date"] + " 00:00:00");
_events[date_time] = _events.update(date_time, (value) {
value.add({
"title": item["title"],
"date": item["date"],
"time": reUse.get_time_am_pm_format(item["time"]),
"feature": item["feature"],
});
return value;
});
}