Home > database >  Flutter if statement not functioning
Flutter if statement not functioning

Time:04-27

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?

  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();
        }
    });
  }

CodePudding user response:

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.

  • Related