How to find duplicate elements length in array flutter? - flutter

I want to implement add to checkout in which number of items added is displayed. Plus button adds elements in list and minus removes elements from list. Goal is just to display particular items added and its quantity. I have added items in list want to count length of duplicate items. How we can do that in flutter?

here is your solution. [Null Safe]
void main() {
List<int> items = [1, 1, 1, 2, 3, 4, 5, 5];
Map<int, int> count = {};
items.forEach((i) => count[i] = (count[i] ?? 0) + 1);
print(count.toString()); // {1: 3, 2: 1, 3: 1, 4: 1, 5: 2}
}

Related

How to map an array into a List of Vector2s in Flutter for FlutterFlame?

I need to convert an array from Firebase into a List of Vector2s in Flutter for FlutterFlame.
From what I gather from the comments you have an array of integers like this:
final numbers = [1, 2, 3, 4, 5, 6, 1, 43];
You can convert this to a list of Vector2 in many ways, this is one way to do it:
final vectors = [
for(int i = 0; i < numbers.length-1; i+=2)
Vector2(numbers[i].toDouble(), numbers[i+1].toDouble())
];

Creating 2D array in Dart where only row length is defined

I want to implement below piece of code(Java) in Dart/Flutter.
Could you please tell me how to achieve this.?
Here rows = 8, colums = not defined.
int[][] myArray = new int[8][];
Dart represents arrays in the form of List objects. A List is simply an ordered group of objects. ... Each element in the List is identified by a unique number called the index.
To get exactly what you want, you could use double List.generate, i.e.:
const cols = 31;
const rows = 12;
final array = List.generate(rows,
(i) => List.generate(cols + 1, (j) => i + j * cols + 1,
growable: false),growable: false);
array.forEach((row) {
print(row);
});
// [1, 32, 63, ...
// [2, 33, 64, ...
// [3, 34, 65, ...
// ...
There is also List.filled, where you can fill the array with some initial value when created. The following init the 2d array with 0s.
final array = List.generate(rows + 1, (i) => List.filled(cols + 1, 0,growable: false), growable: false);

Splitting integer array into multiple chunks in different length in Dart

List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2]
Expected Result:
[[1,2],[3,4,5,6],[7,8,9,10],[11,12,13,14],[15,16]]
I wanna split mainList to 5 chunks with mentioned chunkLength.
How to resolve it in Dart?
You can iterate over the array of your chunks and slice off necessary chunks off the main array. Each iteration your get the chunk itself by using sublist() and then you delete the chunk from the initial array using removeRange()
Here is the code:
List<int> mainList = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];
List<int> chukLengths= [2,4,4,4,2];
List<List<int>> splitIntoChunks(List<int> list, List<int> chunks) {
List<List<int>> result = [];
for (int chunk in chunks) {
result.add(list.sublist(0, chunk));
list.removeRange(0, chunk);
}
return result;
}
void main() {
print(splitIntoChunks(mainList, chukLengths)); // [[1, 2], [3, 4, 5, 6], [7, 8, 9, 10], [11, 12, 13, 14], [15, 16]]
}

Return List of integers from one String consisting of numbers and commas

Dart/Flutter:
I have one string that contains integers, spaces, and maybe commas. How do I convert this to a list of integers?
Example: var StringOfNumbers = one of these strings:
'1, 2, 4, 7,'
'1, 2, 4 7'
'1 2 4 7'
After conversion, all should output to
[1,2,4,7]
I've played with map, parse, and split but can't figure it out.
void main(List<String> args) {
var string = '1, 2, 4, 7,';
var matches = RegExp(r'\d+').allMatches(string);
var myList = matches.map<int>((e) => int.parse(e.group(0))).toList();
print(myList);
}
Result:
[1, 2, 4, 7]
Try this
final string = '1, 2, 4, 7, 1, 2, 4 7 1 2 4 7';
final list = string.split(' ').map((p) => int.tryParse(p.replaceAll(',', '')) ?? 0);

Sort a list in flutter where 0 goes to the bottom

I am trying to sort a list from firebase and I would like for items that have a 0 value to go to the bottom.
So it currently looks like this:
Debts not sorting
I would like for the top item which has a 0 value to go to the bottom and retain the current sorting order.
The code for the query currently looks like this:
.collection('debts')
.document(uid)
.collection('debts')
.orderBy('balance')
.snapshots()
You need to set the flag descending to true, in the orderBy clause. Like this:
.collection('debts')
.document(uid)
.collection('debts')
.orderBy('balance', descending: true)
.snapshots()
You can use .sort() method from List.
Example:
final list = [0, 10, 5, 7, 0, 8, 2];
print(list); // [0, 10, 5, 7, 0, 8, 2]
list..sort((a, b) => a == 0 ? 1 : -1);
print(list); //[10, 5, 7, 8, 2, 0, 0]