Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter if statement not functioning

This should be an easy one but I’ve been stuck for hours.

Situation: I’m trying to execute the signOutProcess to log out of Firebase if the user is not authorized. I’ve set up a field in Firestore, 1 for authorized and 0 for not authorized. It prints the correct result, I can get it to sign out if I remove the if statement but that defeats the purpose.

Question: How do I get the if statement to execute signOutProcess when the nested value is retrieved?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  void getUserAuthorization  () {
    String uid = firebaseAuth.currentUser!.uid;
    print('this is uid $uid');
      FirebaseFirestore.instance
        .collection('Users2022')
        .doc(uid)
        .get()
        .then((DocumentSnapshot documentSnapshot) async {
      dynamic nested = documentSnapshot.get(FieldPath(['Authorized']));
        print('this is the authorization condition $nested');
        if (nested == 0) {
          signOutProcess();
        }
    });
  }

>Solution :

Likely the value you get is '0' and not 0, i.e. it’s a string!

It won’t be equal to the number 0 then, and instead you’d have to write if (nested == '0').

You can try print(nested.runtimeType) to see what you actually got there.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading