Home > Net >  Documentation says "default operator == implementation only considers objects equal if they are
Documentation says "default operator == implementation only considers objects equal if they are

Time:12-05

    void main(){
    String characters='World';
    String a='Hello $characters';
    String b='Hello $characters';
    print(a==b); 

outputs true

    print(identical(a,b));

outputs false on VS code but true on dartPad,I cant understand

    }

CodePudding user response:

The == operator tests whether two objects are equivalent. Two strings are equivalent if they contain the same sequence of code units. See here.

The identical method from the dart:io library returns a Future that completes with the result. Comparing a link to its target returns false, as does comparing two links that point to the same target.

Do you import this method from the dart:io library in VS Code? If so, then it interprets your strings as a path to an object, which then returns false (as marked above).

If so, consider changing the import to dart:core library to simply check with identical() whether two references are to the same object.

  • Related