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

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
}

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

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

Dart combine two Lists in Alternate manner

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

Multidimensional arrays in Swift - Style

There are several idioms for declaring multidimensional arrays in Swift. Consider the following:
var ia1 = Array<Array<Int>>()
var ia2: Int[][] = []
typealias IntArray = Array<Int>
var ia3 = IntArray[]()
var ia4 = Int[][]()
ia1 += [[1, 2, 3], [2, 3, 4]]
ia2 += [[1, 2, 3], [2, 3, 4]]
ia3 += [[1, 2, 3], [2, 3, 4]]
ia4 += [[1, 2, 3], [2, 3, 4]]
let test = (ia1 == ia2) // true
let test2 = (ia3 == ia4) //true
// etc...
Is there actually any difference between the declarations that may bite the developer? And if not, is there any good reason to use one other over the others?
T[] is just syntactic sugar for Array<T> β€” there's no difference in implementation. Which style you prefer is a question of opinion.
Note that depending on what you're trying to model, multidimensional arrays might not be what you're looking for. It might make more sense to use a single array internally, and expose a multidimensional subscript to users of your data structure:
class GameBoard {
let width = 10
let height = 10
let board: [Int]
init() {
board = [Int](count: width * height, repeatedValue: 0)
}
subscript(i: Int, j: Int) -> Int {
return board[i + j * width]
}
}
let b = GameBoard()
b[0,0]
b[4,1]