3
votes

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
5
Can you post the code where you have the issue? I tested a similar case and it is working fine. - Mazin Ibrahim
Are you sure you received string "CREATED" from server? Can you print it out to verify? - Michael Yuwono
@MazinIbrahim I've added the code sample and few other stuff - Michal Takáč
Happens to me too.. comparing two exact value strings (1 from server) returns false... - yonez

5 Answers

2
votes

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

1
votes

I don't have a particular solution to this, but I updated to latest Flutter version that came up today, moved the "CREATED" string into constant and resolved an unrelated warning for another part of the application, and it suddenly started to work.

1
votes

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

The answer for this problem is in the documentation of flutter: https://api.flutter.dev/flutter/dart-core/String/compareTo.html

you can do:

(var.compareTo('WORD') == 0)

are equivalent

.compareTo()

Returns a negative value if is ordered before, a positive value if is ordered after, or zero if and are equivalent.thisother

0
votes

Building off @yonez's answer, the encoding may be different after a string has been passed through a server.

Instead of: String.fromCharCodes(data)

Try using: utf8.decode(data)