Dart combine two Lists in Alternate manner - flutter

I am trying to populate a ListView in Flutter with different sources. So, I have two lists,
list1 = ['a', 'b', 'c']; #The list isn't of numeric type
list2 = ['2', '4'];
Now, I can combine them using the spread operator and get the following output
[a, b, c, 2, 4]
but I want the output to be like -
[a, 2, b, 4, c]
How can this be achieved? What's the most idiomatic approach?

Builtin Iterable has no method zip, but you can write something like:
Iterable<T> zip<T>(Iterable<T> a, Iterable<T> b) sync* {
final ita = a.iterator;
final itb = b.iterator;
bool hasa, hasb;
while ((hasa = ita.moveNext()) | (hasb = itb.moveNext())) {
if (hasa) yield ita.current;
if (hasb) yield itb.current;
}
}
then use zip
final list1 = ['a', 'b', 'c'];
final list2 = ['2', '4'];
final res = zip(list1, list2);
print(res); // (a, 2, b, 4, c)

I guess this:
List list1 = [1, 3, 5];
List list2 = [2, 4];
print([...list1, ...list2]..sort());

Related

How to preserve data after flutter List.add() in 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
}

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
}

Updating variables in dart changes both data

How can I change the value of list2 without changing the value of list1. Changing list2 data updates list1 data.
var list = [1,2,3];
var list1;
var list2;
list1 = list;
list2 = list;
list2[0] = 100;
print(list1); //output [100, 2, 3] // need this to be [1,2,3]
print(list2); //output [100, 2, 3]
Objects are passed by reference. When you do list1 = list; and list2 = list;, you're assigning the reference of list to both list1 and list2. So changing the object at that reference will change the data you see at all of these different variables.
If you want these objects to be modified separately, you need to create a new List instance. This can be done with either List.of or a List literal with the spread operator.
list2 = List.of(list);
or
list2 = [...list];
If you only do this for list2, both list1 and list will still be the same, even if you change only one.
var list = [1, 2, 3];
var list1;
var list2;
list1 = list;
list2 = [...list];
list2[0] = 100;
list1[0] = 200;//list1 is changed here
//But `list` will be changed as well as shown below
print(list); //output [200,2,3]
print(list1); //output [200,2,3]
print(list2); //output [100,2,3]
If you want each of these Lists to be completely separate, you would need to use List.of or a List literal with spread for both list1 and list2.
Use the List.of constructor to assign all elements of list to list2.
Added an example:
var list = [1, 2, 3];
var list1;
var list2;
list1 = list;
// use List.of
list2 = List.of(list1);
list2[0] = 100;
print(
list1); //output [1,2,3]
print(list2); //output [100,2,3]

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);