Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart compare two strings return false

Tags:

flutter

dart

Im new to dart and have a problem during building my Flutter application.

I have a firestore database as a backend and im getting data from there.

When i want to compare part of the data called status with the text 'CREATED', using == comparator, dart will return false.

Can someone explain why and how to check it properly?

rideObject is a Map

enter image description here

Update:

Here is the function that has the condition in it:

Widget _getPage() {
 if (rideObject == null) {
      return OrderRidePage(
          address: address,
          ridesReference: reference,
          setRideReference: this._setRideReference);
    } else {
      print(rideObject['status']);
      if (rideObject['status'] == "CREATED") {
        return LoadingPage(
            removeRideReference: this._removeRideReference,
            rideReference: rideReference);
      } else {
        return RidePage(
            address: address,
            ridesReference: reference,
            setRideReference: _setRideReference);
      }
    }
  }

The print statement returns to output:

I/flutter (15469): CREATED

Here you can see the structure of the rideObject

enter image description here

Funnily enough, the rideObject["status"] is String type as shown in here in console:

rideObject["status"] is String
true
"CREATED" is String
true
rideObject["status"]
"CREATED"
rideObject["status"] == "CREATED"
false
like image 740
Michal Takáč Avatar asked Jun 14 '26 16:06

Michal Takáč


2 Answers

The String you got from your server is probably encoded and contains special character which you can't see, try to compare the hex values of both of the strings, and then replace all the special characters from the String returned by the server.

Using this, you can see the actual non visible difference between the two strings:

var text1 = utf8.encode(hardcodedText).toString();
var text2 = utf8.encode(textFromServer).toString();
like image 123
yonez Avatar answered Jun 17 '26 11:06

yonez


If both are really strings, you can use "compareTo" which will return 0 if both are equal.

if(str1.compareTo(str2)==0){
}

It is explained here: https://www.tutorialspoint.com/dart_programming/dart_programming_string_compareto_method.htm

like image 40
Celt K. B. Avatar answered Jun 17 '26 12:06

Celt K. B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!