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

Time:12-12

    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.

CodePudding user response:

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