suppose i have a list:
List? myList = [{Person: 1},{Person: 1},{Person: 2},{Person: 3},{Person: 4},{Person: 5}];
I would like to remove the first occurrence of '1' using list.removeWhere.
How can I achieve this? I tried this one below but it removed all '1's.
myList.removeWhere((e) => e['Person'] == 1);
You can get the first item then remove it from list. removeWhere removes all elements that satisfy its condition
void main(List<String> args) {
List? myList = [
{"Person": 1},
{"Person": 1},
{"Person": 2},
{"Person": 3},
{"Person": 4},
{"Person": 5}
];
final item = myList.firstWhere((e) => e['Person'] == 1);
myList.remove(item);
print(myList);
}
Or
final int item = myList.indexWhere((e) => e['Person'] == 1);
myList.removeAt(item);
Related
Dart Can't change map value from initial map
I have this code :
void main() {
Map tabtest = {
'1' : {
'd' : 'one'
}
};
Map tabtest1 = {};
tabtest.forEach((cle1, val1) {
val1['d']='two';
tabtest1['cle1']=val1;
});
Map tabtest2={};
tabtest.forEach((cle2, val2) {
val2['d']='three';
tabtest2['cle2']=val2;
});
print(tabtest);
print(tabtest1);
print(tabtest2);
}
This code display :
{1: {d: three}}
{1: {d: three}}
{1: {d: three}}
But I want different value from tabtest1 and tabtest2 like :
tabtest1 = {1: {d: two}}
tabtest2 = {1: {d: three}}
Can help me?
Thanks.
There were a couple of problems:
you were actually using the same sub-map {'d': 'one'} in all three versions, instead of creating three seperate maps. The easiest way to copy a Map is to use Map secondMap = Map.from(firstMap). Beware though - this will only copy the first 'level' of the map. See this question for more information
you were using the string 'cle1' instead of it's value, so tabtest1['cle1'] should have been tabtest1[cle1]
void main() {
Map tabtest = {
'1' : {
'd' : 'one'
}
};
Map tabtest1 = {};
// For each map entry in tabtest
tabtest.forEach((cle, val) {
// First COPY the sub-map. If we don't make a copy, we end up
// changing the sub-map in both tabtest and tabtest2
Map val1 = Map.from(val);
// Then we can update the value in our new sub-map:
val1['d']='two';
// And set this as the key '1' in tabtest1
tabtest1[cle]=val1;
});
Map tabtest2={};
tabtest.forEach((cle, val) {
Map val2 = Map.from(val);
val2['d']='three';
tabtest2[cle]=val2;
});
print(tabtest);
print(tabtest1);
print(tabtest2);
}
{1: {d: one}}
{1: {d: two}}
{1: {d: three}}
Here's my data in Firebase:
prices
price1
priceAmount1: 10
priceAmount2: 20
priceAmount3: 30
price2
priceAmount1: 15
priceAmount2: 5
priceAmount3: 7
price3
priceAmount1: 2
priceAmount2: 4
priceAmount3: 6
If I straight away retrieve it. Sometimes the ordering is price2, price3, price1. So I tried sorting it like this:
var sortedKeys = prices!.price!.keys.toList()..sort();
for (var it = 0; it < sortedKeys.length; it++) {
print('${sortedKeys[it]}');
}
But since it becomes a list, I'm not able to access the map anymore. I wanted to sort the map within a map then store it in a list within a list. Please help. Thanks!
Edit:
final splayByPrice =
SplayTreeMap<dynamic, dynamic>((a, b) => a.compareTo(b));
List<Map<dynamic, dynamic>> priceAmountMap = [
{
'priceAmount1': 10,
'priceAmount2': 20,
'priceAmount3': 30,
},
{
'priceAmount1': 15,
'priceAmount2': 5,
'priceAmount3': 7,
},
{
'priceAmount1': 2,
'priceAmount2': 4,
'priceAmount3': 6,
}
];
Map<dynamic, Map<dynamic, dynamic>> priceMap = {
'price2': priceAmountMap[1],
'price1': priceAmountMap[0],
'price3': priceAmountMap[2],
};
priceMap.forEach((key, value) {
splayByPrice.addAll(value);
});
priceMap.forEach((key, value) {
print('$key \t $value');
});
What it prints, is still not sorted.
price2 {priceAmount1: 15, priceAmount2: 5, priceAmount3: 7}
price1 {priceAmount1: 10, priceAmount2: 20, priceAmount3: 30}
price3 {priceAmount1: 2, priceAmount2: 4, priceAmount3: 6}
Dart's default Map implementation is a LinkedHashMap that stores entries in order of insertion.
(You haven't provided code that clearly shows your data structure, so I'll use my own example.)
If you want the Map to be sorted by the keys' alphabetic order, then you could:
Create a new Map, adding items to it in the desired order:
var theMap = {
4: 'four',
7: 'seven',
6: 'six',
9: 'nine',
5: 'five',
1: 'one',
8: 'eight',
0: 'zero',
3: 'three',
2: 'two',
};
void main() {
var sortedKeys = theMap.keys.toList()..sort();
var sortedMap = {
for (var key in sortedKeys)
key: theMap[key]!,
};
print(sortedMap);
}
Update the existing Map, removing and re-adding items in the desired order:
void main() {
var sortedKeys = theMap.keys.toList()..sort();
for (var key in sortedKeys) {
var value = theMap[key]!;
theMap.remove(key);
theMap[key] = value;
}
print(theMap);
}
Use a SplayTreeMap, which keeps entries sorted by a comparison rule.
import 'dart:collection';
void main() {
var splayTreeMap = SplayTreeMap.of(theMap);
print(splayTreeMap);
}
If you need to sort the data only once, I'd use one of the first two methods. If you're frequently adding new entries, then I'd use a SplayTreeMap instead of a Map/LinkedHashMap so that entries are kept in a sorted order. (But note that lookups, insertions, and removals in a SplayTreeMap are O(log n) instead of O(1).)
Also, if you're sorting strings that contain numbers, you probably don't want to use the normal string sort.
map1 = { "a": 10, "b": 6 },
map2 = { "a": 10, "b": 6, "c": 7, "d": 8 };
Flutter:How to merge two objects and sum the values of the same key?
Do forEach on the longest map and check if the small map contains the key if it does then update the value with the sum or add the new.
map2.forEach((key, value) {
if (map1.containsKey(key)) {
map1[key] = value + map1[key]!;
} else {
map1[key] = map2[key]!;
}
});
map1 will be the final result.
So, if you want to combine/merge the two maps use this code this answer:
final firstMap = {"1":"2"};
final secondMap = {"2":"3"};
final thirdMap = { // here simple adding element to map
...firstMap,
...secondMap,
};
but if you want to make sum and merge use this :
map2.forEach((k, v) {
if (map1.containsKey(k)) { // check if the map has more then 2 values as the 1st one
map1[k] = v + map1[k]!; // if yes so make the some
} else {
map1[k] = map2[k]!; // if no then add the values to map
}
});
as MEET Prajapati asnwer.
So I have a List of Maps of two dynamic variables.
[{Title: product 1, Item Score: 87.3, Characters: 72, Image Count: 6},
{Title: product 2, Item Score: 85.1, Characters: 56, Image Count: 2}]
How would I add up all of them item scores? There are more items than this in the full file.
I would look at fold in the standard library https://api.dart.dev/stable/1.10.1/dart-core/List/fold.html.
With fold, you iterate over the list of maps, and the first argument is your starting value. In the case of sum, this would be 0. Your inner function in the fold takes 2 arguments, the current accumlation(sum), and the item in your list that is currently being iterated over.
E.g.:
const itemScores = [{'itemScore': 1}, {'itemScore': 2}, {'itemScore': 44}];
var sum = itemScores.fold(0, (i, el){
return i + el['itemScore'];
});
Where i is the current value of the accumulator, and el is the current item in the list being iterated over.
Assuming that, you need to store the value in some variable, you can simply use List.forEach((element){ })
Advantage, you can iterate over as many data as you can, and then store the value
void main(){
var data = [{"Title": "product 1", "Item Score": 87.3, "Characters": 72, "Image Count": 6}, {"Title": "product 2", "Item Score": 85.1, "Characters": 56, "Image Count": 2}];
double totalScores = 0.0;
// looping over data array
data.forEach((item){
//getting the key direectly from the name of the key
totalScores += item["Item Score"];
});
print(totalScores); // OUTPUT ==> 172.39999999999998
}
List arr = [[1,2,3],[4,5,7],[8,9,10],[14,15,19]];
int stairsIn203(List<List<int>> arr) {
// your code here
int _a = 0 ;
List _b = [];
for(List i in arr){
_b += i;
}
_a = _b.reduce((value, element) => value + element) * 20;
return _a;
}
Or
int stairsIn20(List<List<int>> arr) => arr.expand((e) => e).reduce((v, e) => v + e) * 20;
var sum = [{Title: product 1, Item Score: 87.3, Characters: 72, Image
Count: 6}, {Title: product 2, Item Score: 85.1, Characters: 56, Image
Count: 2}]].reduce((item1, item2) => item1["Item Score"] +item2["Item
Score"]);
How to search a list of a class object with one of its property matching to any value in another list of strings
I am able to get filtering based on a single string , but not on a list of strings
final List<shop_cart.ShoppingCart> cartprd = snapshot.documents
.map((f) => shop_cart.ShoppingCart.fromMap(f.data))
.toList();
List<SomeClass> list = list to search;
List<String> matchingList = list of strings that you want to match against;
list.where((item) => matchingList.contains(item.relevantProperty));
If the number of items in list is large, you might want to do:
List<SomeClass> list = list to search;
List<String> matchingList = list of strings that you want to match against;
final matchingSet = HashSet.from(matchingList);
list.where((item) => matchingSet.contains(item.relevantProperty));
Or else just always store the matching values as a hashset.
In case if you want to check for a value in a list of objects . you can follow this :
List rows = [
{"ags": "01224", "name": "Test-1"},
{"ags": "01224", "name": "Test-1"},
{"ags": "22222", "name": "Test-2"},
];
bool isDataExist(String value) {
var data= rows.where((row) => (row["name"].contains(value)));
if(data.length >=1)
{
return true;
}
else
{
return false;
}
}
you can put your own array of objects on rows . replace your key with name . you can do your work based on true or false which is returned from the function isDataExist
As of today, you can't.
(A side note : You can use .where, .singleWhere, .firstWhere. This site explains various list/array methods.)
You can simply use List.where() to filter a list
final List<shop_cart.ShoppingCart> cartprd = snapshot.documents
.where((f) => shop_cart.ShoppingCart.contains(f.data));
var one = [
{'id': 1, 'name': 'jay'},
{'id': 2, 'name': 'jay11'},
{'id': 13, 'name': 'jay222'}
];
int newValue = 13;
print(one
.where((oldValue) => newValue.toString() == (oldValue['id'].toString())));
OUTPUT : ({id: 13, name: jay222})
store output in any variable check if variable.isEmpty then new value is unique either
var checkValue = one
.where((oldValue) => newValue.toString() == (oldValue['id'].toString()))
.isEmpty;
if (checkValue) {
print('Unique');
} else {
print('Not Unique');
}
OUTPUT : Not Unique