8

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
4
  • Can you post the code where you have the issue? I tested a similar case and it is working fine. Commented May 19, 2019 at 11:10
  • Are you sure you received string "CREATED" from server? Can you print it out to verify? Commented May 19, 2019 at 12:51
  • @MazinIbrahim I've added the code sample and few other stuff Commented May 19, 2019 at 21:21
  • Happens to me too.. comparing two exact value strings (1 from server) returns false... Commented Oct 13, 2020 at 21:18

5 Answers 5

5

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();
Sign up to request clarification or add additional context in comments.

1 Comment

The BEST WAY to do it ...
5

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

3 Comments

Did not help, returns -1
What happens if you save the two Strings in two String-Variables before comparing them? String str1 = rideObject["status"] ; String str2 = "CREATED"; and then: str1.compareTo(str2);
@celt-k-b same issue again.
3

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.

Comments

1

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

1 Comment

Welcome to Stack Overflow! Please edit your answer so that the code is formatted as code.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.