How to remove null and 0 from the map? - flutter

I tried to remove NULL from the map for a long time.....
sortedMap.removeWhere((key, value) => value == "NULL");
sortedMap.removeWhere((key, value) => value == NULL);
...however, this could not be done, and I decide to set the default value "No Data", this solved the problem.
sortedMap.removeWhere((key, value) => value == "No Data");
and, this solved the problem! Now, I can’t remove the value "0" from the map now.
sortedMap.removeWhere((key, value) => value == "0");
sortedMap.removeWhere((key, value) => value == 0);
This's my map:
{h_1: 0, h_2: 1, h_3: 1, h_4: 1, h_5: 0, h_6: 1}
Please, tell me.
How to solve this problem?
Thanks for any help

Is it same solution like yours?
In my case, it works well.
void main() {
Map data = {'h_1': 0, 'h_2': 1, 'h_3': 1, 'h_4': 1, 'h_5': 0, 'h_6': 1};
print(data);
data.removeWhere((k, v) => v == 0);
print(data);
}

What is NULL? Is it perhaps null you are looking for to remove?
(Null in the case below is a class from dart:core)
import 'package:flutter_test/flutter_test.dart';
void main() {
test(
'Remove null',
() {
Map<String, dynamic> map = {
'a': 1,
'b': 2,
'c': null,
'd': 4,
};
print(map);
map.removeWhere((key, value) => value == Null);
print(map);
map.removeWhere((key, value) => value == null);
print(map);
// assert
},
);
}
This prints:
{a: 1, b: 2, c: null, d: 4}
{a: 1, b: 2, c: null, d: 4}
{a: 1, b: 2, d: 4}
✓ Remove null

Related

How to join the values in-between two values in a list

I have this list [null, 3, 5, null] and I want to join the values in-between nulls and put it into the same list
For example:
[null, 3, 5, null] into [null, 35, null]
I made this one extension that groups all the values between two nulls
extension GroupByNull<T> on List<T> {
List<List<T>> groupByNull() {
final groupedList = <List<T>>[];
var list = this;
for (var i = 0; i < list.length; i++) {
if (list[i] == null) {
if (i > 0) {
groupedList.add(list.sublist(0, i));
}
list = list.sublist(i + 1);
i = 0;
}
}
if (list.isNotEmpty) {
groupedList.add(list);
}
return groupedList;
}
}
Which returns [[3, 5]] for [null, 3, 5, null]... But I want it to be joined together and added to the original list in the same index
How can I solve this...
thank you.
Note that you can't operate on an arbitrary List<T> because there's no general way to combine two elements of some arbitrary type T. What you want could make sense for List<int?> or maybe List<String?>. (Or maybe it could work on an arbitrary List<T> input if you want a List<String?> output.)
Assuming that you want to operate on List<int?>, then basically as you iterate over your input list, keep track of your current accumulated value. If you encounter null, add the current value (if any) to your output List along with the null. Don't forget to add the current accumulated value (if any) when you're done iterating in case there isn't a final null element.
extension GroupByNull on List<int?> {
List<int?> groupByNull() {
var result = <int?>[];
int? currentValue;
for (var element in this) {
if (element != null) {
currentValue = (currentValue ?? 0) * 10 + element;
} else {
if (currentValue != null) {
result.add(currentValue);
currentValue = null;
}
result.add(currentValue);
}
}
if (currentValue != null) {
result.add(currentValue);
}
return result;
}
}
void main() {
print([null, 3, 5, null].groupByNull()); // Prints: [null, 35, null]
print([3, 5, null].groupByNull()); // Prints: [35, null]
print([3, 5, null, null].groupByNull()); // Prints: [35, null, null]
print([null, 3, 5].groupByNull()); // Prints: [null, 35]
print([null, null, 3, 5].groupByNull()); // Prints: [null, null, 35]
print([null, 0, 0, 0, null].groupByNull()); // Prints: [null, 0, null]
print([null, 1, 2, null, 3, 4, null]
.groupByNull()); // Prints: [null, 12, null, 34, null]
print([null].groupByNull()); // Prints: [null]
print([null, null].groupByNull()); // Prints: [null, null]}
}
You can use this solution.
extension GroupByNull<T> on List<T> {
List groupByNull() {
final groupedList = [];
var list = this;
list.removeWhere( (value) => value == null); // remove all null values
groupedList.add(int.parse(list.join(""))); // combine number left in list
groupedList.insert(0,null); // add null
groupedList.add(null);
return groupedList;
}
}

I want to reverse the key and value of Map

How can I reverse the key and value of the map?
for example, {1:a , 2:b , 3:c} => {a:1 ,b:2 ,c:3}
Try this piece of code:
Map<int, String> map = {1: "a", 2: "b", 3: "c"};
Iterable<String> values = map.values;
Iterable<int> keys = map.keys;
Map<String, int> reversedMap = Map.fromIterables(values, keys);
print(reversedMap); // {a:1 ,b:2 ,c:3}
You can do this:
const items = {1:'a' , 2:'b' , 3:'c'};
void main() {
final inverted = items.map((key, value) => MapEntry(value, key));
print(inverted);
}
It logs
{a: 1, b: 2, c: 3}

Dart Flutter: How to compare int values of a Map?

Say that I have a Map<String, int> containing entries and I would like to select the first entry with the highest value, see the example below:
Map<String, int> wordCount = {
'foo' : 3,
'bar' : 3,
'john' : 4,
'doe' : 3,
'four' : 4
}
What is the most efficient way to get john as it has the first highest value?
You can use something like this:
print(wordCount.entries.reduce((maxEntry, entry) {
return maxEntry.value < entry.value ? entry : maxEntry
}).key);
Something like this?
void main() {
Map<String, int> wordCount = {
'foo': 3,
'bar': 3,
'john': 4,
'doe': 3,
'four': 4
};
final nameWithHighestValue =
wordCount.entries.reduce((a, b) => a.value >= b.value ? a : b).key;
print(nameWithHighestValue); // john
}

Can I sort List<dynamic> in dart

Can I sort List<dynamic> in dart?
List<dynamic> list= [9,10,'Plus One'];
list.sort();
print(list);
I expect the result like 9,10,'Plus One' Or 'Plus One', 9, 10
You just need to provide a callback to List.sort that orders heterogeneous types the wya you want. For example, assuming that your heterogeneous List contains only ints and Strings, you could do:
List<dynamic> list = [9, 10, 'Plus One'];
list.sort((a, b) {
if ((a is int && b is int) || (a is String && b is String)) {
return a.compareTo(b);
}
if (a is int && b is String) {
return -1;
} else {
assert(a is String && b is int);
return 1;
}
});
print(list);
If you need to potentially handle other types, you will need to adjust the callback appropriately.
If you want to sort dynamic list and want string before number(int or double) try
this code:
List<dynamic> list = [
'mahmoud',
14,
'zika',
9,
10,
'plus One',
5,
'banana',
1,
2.5,
'apple',
2,
1.2,
'ball'
];
list.sort(
(a, b) {
if ((a is num && b is num) || (a is String && b is String)) {
return a.compareTo(b);
}
// a Greater than b return 1
if (a is num && b is String) {
return 1;
}
// b Greater than a return -1
else if (a is String && b is num) {
return -1;
}
// a equal b return 0
return 0;
},
);
print(list);// [apple, ball, banana, mahmoud, plus One, zika, 1, 1.2, 2, 2.5, 5, 9, 10, 14]

Conditionally add to Map

I have a Map to which I would like add elements only if they meet a condition.
For example, if for keys a and b, is there something that works like this?
Map myMap = {
if a is not null "a" : a,
if b is not null and be is not empty "b": b,
}
So a and b would not be in the map unless the condition is met
For example for react is goes something like this
const myMap = {
...(a != null && { a: a }),
...(b != null && b.length > 0 && { b: b }),
};
Try this in dartpad:
void main() {
for (var a in [null, 15]) {
for (var b in [null, '', 'hello']) {
var myMap = {
if (a != null) 'a': a,
if (b != null && b.isNotEmpty) 'b': b,
};
print('a = $a, b = $b, myMap = $myMap');
}
}
}
You can do absolutely the same in Dart!
return {
if (true) ...{'key': value},
};
Dartpad is very useful
void main() {
Map<String, String> myMap = new Map();
Map<String,String> array = {"a":"apple","b":"g"};
array.forEach((key,value){
value.length> 0 ? myMap.addAll({key: value}): "";
});
print(myMap);
}