I have a list of map and I want to get the map of specific key
for example :
video of letter a
List<Map<String, String>> letters = const [
{
'letter': 'a',
'name' : 'ddd',
'video' : 'ss',
},
{
'letter': 'b',
'name' : 'ddd',
'video' : 'ss',
},
{
'letter': 'c,
'name' : 'ddd',
'video' : 'ss',
},
]
I guess you can use .where method like this
List listWithVideo = letters.where((element) => element['letter'] == 'a').toList();
Now here you will get list of maps where you will find your letter a.
If you want to get only one map or the first map that has the same, you can also use firstWhere method.
I am also beginner,but here is my effort
void main() {
List<Map<String, String>> letters = const [
{
'letter': 'a',
'name': 'ddd',
'video': 'ss',
}
,
{
'letter': 'b',
'name' : 'ddd',
'video' : 'ss',
},
{
'letter': 'c',
'name' : 'ddd',
'video' : 'ss',
},
];
Map<String,dynamic> lst=letters.firstWhere((element) {
return element['letter']=='a';
});
print(lst['video']);
}
its showing correct output
Try this:
Map<String, dynamic> getAMap() {
var list = letters.where((element) => element["letter"] == "a").toList();
return list.isNotEmpty ? list.first : {};
}
I found a way to do this , so i can use the index and get the data that i want from the map
int search (String letter){
int index=0;
for ( var i=0 ; i<list.length;i++ )
{
if (list[i]['letter']==letter){
index=i;
}
}
return index;
}
Related
I have customer collection inside I have list of merchant data like
{
'customer':'xyz',
'contactNumber':'999999999',
'merchantMap':[
{
'merchantName':'abx',
'merchantId':'dsdfsbdmmm'
},
{'merchantName':'abx',
'merchantId':'dsdfsbdmmm'
}
]
}
Here inside merchantMap I want to update his name by checking his merchantId.
How can I achieve this?
Lets assume your customer data is this:
var customerData = {
'customer': 'xyz',
'contactNumber': '999999999',
'merchantMap': [
{'merchantName': 'abx', 'merchantId': '12'},
{'merchantName': 'abx', 'merchantId': '23'}
]
};
and your specific id is this:
String selectedId = "23";
you can do this to change the item that contains that id:
var newMerchantMap =
(customerData["merchantMap"] as List<Map<String, dynamic>>)
.map((e) => e['merchantId'] == selectedId
? {'merchantName': 'newNAme', 'merchantId': e["merchantId"]}
: e)
.toList();
customerData['merchantMap'] = newMerchantMap;
result :
{
customer: xyz,
contactNumber: 999999999,
merchantMap: [
{merchantName: abx, merchantId: 12},
{merchantName: newNAme, merchantId: 23}
]
}
i want to use table_calendar with firebase data, but still having issue passing events into the widget ....
Sample structure of firebase data:
{
'date' : 26 Jun
'name' : A
'date' : 26 Jun
'name' : B
'date' : 11 Feb
'name' : C
}
i want to create the Map in this way:
{
'26 Jun' : [{'date' : 26 Jun, 'name' : A} , {'date' : 26 Jun, 'name' : B}]
'11 Feb' : [{'date' : 11 Feb, 'name' : C}]
}
below code was trying to use Map.fromIterable but i'm not sure how to merge data with same key...
final _kEventSource = Map<DateTime, List<dynamic>>.fromIterable(bookings,
key: (item) => item['date'].toDate(),
value: (item) => List.generate(1, (index) => item))
================================================================
UPDATE:
var newMap = groupBy(bookings, (obj) => (obj as Map)['date'].toDate()).map((k, v) => MapEntry<DateTime, List<dynamic>>(
k,
v.map((item) {
item.remove('date');
return item;
}).toList()));
print(newMap);
final kEvents = LinkedHashMap<DateTime, List<dynamic>>(equals: isSameDay, hashCode: getHashCode)
..addAll(newMap);
return kEvents[day] ?? [];
}
i cannot execute the code above , seeing error with
The method 'toDate' was called on null.
Receiver: null
Tried calling: toDate()
With the collection package you can group by date. Try this code:
import "package:collection/collection.dart";
void main() {
var data = [
{'date': "26 Jun", 'name': "A"},
{'date': "26 Jun", 'name': "A"},
{'date': "11 Feb", 'name': "C"},
];
var newMap = groupBy(data, (Map obj) => obj['date']).map((k, v) => MapEntry(
k,
v.map((item) {
item.remove('date');
return item;
}).toList()));
print(newMap);
}
Output:
{26 Jun: [{name: A}, {name: A}], 11 Feb: [{name: C}]}
I want to add datas I pull from firebase.I store my data in sting clases . I want to pull that data from firebase. and add to local map list so I can acces quickly and easly.
` dynamic getData(Map data, List<String> way) {
dynamic dataTemp = data;
if (way.length > 0) {
for (int x=0; x < way.length; x++) {
dataTemp = dataTemp[way[x]];
}
}
return dataTemp;
}
List<Map<String, dynamic>> locations = [
{
'country': 'Japan',
'city': 'Tokyo',
'Latitude': 35.6762,
'Longitude': 139.6503,
'utcOffset': 9,
'example' : {
'data' : "text",
'number' : 20,
'boolean': false
}
}
];
getData(locations[0],["example","number"]);`
in this example how can I add multiple of .
` {
'country': 'Japan',
'city': 'Tokyo',
'Latitude': 35.6762,
'Longitude': 139.6503,
'utcOffset': 9,
'example' : {
'data' : "text",
'number' : 20,
'boolean': false
}
}`
this code
from my firebase database ?
Use .asMap()
Example:
List mylist = [];
mylist.add(locations);
final myMap = mylist.asMap();
print(myMap[0][0]['example']); // {data: text, number: 20,boolean: false}
print(myMap[0][0]['example']['number']); // 20
I have a List<Map<String, String>> like below
[
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
]
And, the ID list **List** as ['bb', 'aa']. By using the ID list, I want to return a new list ['Jane', 'John'] as **List _selectedList**.
I have tried to do it with the .**indexWhere**, however, I am stuck on the List where it has more than one value.
How can I return the List only with the name-value when there is more than one value to look for?
void main() {
var a = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var b = ['bb', 'aa'];
var c = a.where((m) => b.contains(m['id'])).map((m) => m['name']);
print(c);
}
Result
(John, Jane)
Use a set to filter out the IDs efficiently.
var ids = ["aa", "cc"];
var idSet = Set<String>.from(ids);
var json = [
{ 'name': 'John', 'id': 'aa' },
{ 'name': 'Jane', 'id': 'bb' },
{ 'name': 'Lisa', 'id': 'cc' },
];
var _selectedList = json.where((data) => idSet.contains(data["id"]))
.map((data) => data["name"]).toList();
print(_selectedList);
Here, .where filters out the data where the ID matches one in the input list "IDs". Sets make the process efficient. Then, the resultant objects are passed to .map where the "name" field is extracted. Finally, the whole thing is converted into a list thus returning the list of names.
Output of the above code:
[John, Lisa]
As the title suggests, I am upgrading to ES5. My original ES query looked like:
my $response = $elastic->do_request_new(
{
query => {
filtered => {
filter => {
or => [
{ term => { _type => { value => "some_val1" } } },
{ term => { _type => { value => "some_val2" } } },
]
},
query => {
query_string => {
query => $qstring,
rewrite => "scoring_boolean",
analyze_wildcard => "true",
}
}
}
},
sort => [ qw(_score) ],
size => 50,
},
);
My updated looks like:
my $response = $elastic->do_request_new(
{
query => {
bool => {
should => [
{ term => { _type => { value => "some_val1" } } },
{ term => { _type => { value => "some_val2" } } },
],
must => {
query_string => {
query => $qstring,
rewrite => "scoring_boolean",
analyze_wildcard => "true",
}
}
}
},
sort => [ qw(_score) ],
size => 50,
},
);
However, upon searching for exact strings in my elastic database, I am returning zero results:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
Any ideas about what might be happening? My guess is I have my query structure wrong. Thanks!
Update: The following fixed query:
my $response = $elastic->do_request_new(
{
query => {
bool => {
must => [
{
query_string => {
query => $qstring,
rewrite => "scoring_boolean",
analyze_wildcard => "true",
},
}
],
filter => {
bool => {
should => [
{ term => { _type => { value => "some_val1" } } },
{ term => { _type => { value => "some_val2" } } },
],
},
},
},
},
sort => [ qw(_score) ],
size => 50,
},
);