How to preserve data after flutter List.add() in flutter - flutter

Store data in a 1-dimensional List and add() it to a 2-dimensional List.
After add(), .clear() the one-dimensional List.
However, since List refers to an address, clear() loses the address, so the 2D array becomes an empty List. Is there a way to keep the values ​​in a 2D List even after clearing?
List<int> num = [1, 2, 3];
List<List<int> num2 = [];
num2.add(num);
num.clear();

You could use List.from() to perform shallow copy
List<int> num = [1, 2, 3];
List<List<int>> num2 = [];
num2.add(List.from(num));
print(num2); //[[1, 2, 3]]
num.clear();
print(num2); //[[1, 2, 3]]

use trick with toList() method
void main() {
List<int> num1 = [1, 2, 3];
List<List<int>> num2 = [[0,2]] ;
num2.add(num1.toList());
num1.clear();
print(num2); // [[0, 2], [1, 2, 3]]
print(num1); // []
}

You should add list as anonymous list
List<int> num = [1, 2, 3];
List<List<int>> num2 = [];
// add as anonymous list
num2.add(num.toList());
num.clear();
print(num2.length); // 1
print(num.length); // 0
for (var element in num2) {
print(element.length); // 3
}

Related

check if items in array existed and add new values from another array without overwriting?

I want to check if items in array existed and add new values from another array without overwriting element after reloading. I created such code:
//take from that array
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
list.forEach((item) {
if(!list.contains(item)){
newValueInt.add(item);
print(newValueInt);
}
});
and it shows me that print:
[2, 6, 7, 3]
[2, 6, 7, 3, 5]
List<int> list = [2, 3, 5];
// add to this array and check if this array already has the same element or not
List<int> newValueInt = [2, 6, 7];
List<int> temp = [];
for (var item in list) {
if(!newValueInt.contains(item)){
temp.add(item);
}
}
List<int> result = newValueInt + temp;
print(result); //---> [2, 6, 7, 3, 5]
if(!list.contains(item)){
to
if(! newValueInt.contains(item)){

Flutter: doing math to each item in a list?

How do we do math to each item in a list (add numbers from variable and multiply final value) and return another List?
Something like this:
var list = [1, 2, 3, 4, 5];
print(list); // Prints [1, 2, 3, 4, 5]
var list2 = list.map((i) => i * 2 + 5 /* math here*/).toList();
print(list2); // Prints [7, 9, 11, 13, 15]

I want to calculate each number from the array in flutter

I have an array in the flutter
final list = [1, 2, 3, 4, 5];
I want to loop through the array to calculate the sum of all the items in the array,how i can?
Try the following:
final list = [1, 2, 3, 4, 5];
final result = list.reduce((sum, element){
return sum + element;
});
This could do it
final sum = list.fold<int>(0, (previousValue, number) => previousValue + number);

Are int, double, String constants and canonicalized by default in dart?

void main(){
int a=3;
int b=3;
print(identical(a,b));
returns true
double c=3.2;
double d=3.2;
print(identical(c,d));
returns true, same for String type
List l1=[1,2,3];
List l2=[1,2,3];
print(identical(l1,l2));
}
But for list returns false.How?.As int,double,string override ==operator does that have anything to do with identical returning true for these types.
You got the false because a list is an indexable collection of objects with a length. In identical checks, two instances are the same or not but you can convert this instance into a string.
List l1 = [1, 2, 3, 4];
List l2 = [1, 2, 3, 4];
print(identical(l1.toString(), l2.toString())); //true
For list comparison, you can use listEquals
import 'package:flutter/foundation.dart';
void main() {
List<int> l1 = [1, 2, 3,4];
List<int> l2 = [1, 2, 3, 4];
print(listEquals(l1, l2)); //true
}

Dart/Flutter - Keep only duplicate items in List

So I'm working on an app in Flutter, and long story short: I have 2 'filter options' which create 2 seperate lists. Now what I want is to use those 2 Lists and find the items which are present in BOTH lists and add that item to a third List.
Example:
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
Result: [1, 2]
I know I could potentially loop through 1 list and then check with 'contains()' if the item is present in the other list. But it could be that 1 (or both) lists are empty and then my third list will be empty as I simply will never loop to add a duplicate item to the third list
Something like this?
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = [...first_list.where(second_list.contains)];
print(shared); // [1, 2]
}
What you need is intersection between two collections. Most likely it makes sense not to use lists, but rather sets of items.
I would solve this like that:
void main() {
List<int> first_list = [1, 2, 3, 4, 5];
List<int> second_list = [1, 2, 8, 9];
final shared = first_list.toSet().intersection(second_list.toSet());
print(shared);
}
If you can work with sets, not with lists it would be even simpler:
void main() {
Set<int> first_list = {1, 2, 3, 4, 5};
Set<int> second_list = {1, 2, 8, 9};
final shared = first_list.intersection(second_list);
print(shared);
}
You can use intersection
List<int> firstList = [1, 2, 3, 4, 5];
List<int> secondList = [1, 2, 8, 9];
final firsToSet = firstList.toSet();
final secondToSet = secondList.toSet();
final res = firsToSet.intersection(secondToSet);
print(res);