-1

When i try to check whether a particular number exists in firebase collection or not I always get a null in return. What is the reason for that?

How can I overcome this issue so that the function _numberCheck() returns a Boolean value which I can use for further processing?

The code for the registration page is as follows:

    Future<bool> _numberCheck(mobileno) async {
    print("Start of the function");
    db
        .collection("transcriber_user_registeration")
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      snapshot.documents.forEach((f) async {
        isPresent = true;
        isPresent = false;
        if ('${f.data['userid']}' == mobileno) {
          setState(() {
            isPresent = true;
          });
        } else {
          setState(() {
            isPresent = false;
          });
        }
      });
    });
//    print(isPresent);
    return isPresent;
  }
Pranav Arora
  • 3
  • 2
  • 6

2 Answers2

0

You function returns null because you are returning isPresent without setting it to true, you only set isPresent = false, which means isPresent will be null otherwise, as variables hold objects references, so it will hold null if you didn't initialize it.

bool isPreset; // isPreset is null
isPreset = true; // isPreset is true
isPreset = false; // isPreset is false 

If a variable lacks a value, it's value is null. Always initialize your variables to something.

Also, if f.data['userid'] is a String, you don't have to make it a String again by surrounding it with '${}'

Change your code to:

Future<bool> _numberCheck(mobileno) async {
   QuerySnapshot snapshot = await db.collection("transcriber_user_registeration")
        .getDocuments();
     snapshot.documents.forEach((f) {
        if (f.data['userid'] == mobileno) {
          return false;
        }
      });

    return true;
  } 

And call setState for the result of this function where you call it. Or just use a FutureBuilder to deal directly with the result and refresh your screen automatically.

If it returns true it means the query collection result is an empty collection or the if (f.data['userid'] == mobileno) is never true. So check the values on both.

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173
  • thanks for the tip, made the changes. However, my database consists of a this collection and this field then why is it returning null then also? What should I do so that it returns a boolean value? – Pranav Arora Apr 02 '20 at 05:56
  • You don't declare `isPresent` please fix your code, remove the unnecessary prints and comments – Michel Feinstein Apr 02 '20 at 06:00
  • fixed the code, removed the unnecessary prints and comments – Pranav Arora Apr 02 '20 at 06:40
  • i just want to query my collection and check whether a particular mobile number exists or not if exists then simply take him to the login page and if not then first store his details and then take him to the login page how can I accomplish this using this function? – Pranav Arora Apr 02 '20 at 07:44
  • even after initialising it is giving null when I'm printing it – Pranav Arora Apr 02 '20 at 17:48
  • See my update and double check the Firestore collection name is correct and there's data there. – Michel Feinstein Apr 02 '20 at 18:04
  • yes data is there in the collection and double checked your answer. Is there any way to connect? – Pranav Arora Apr 02 '20 at 18:26
  • What do you mean by connect? – Michel Feinstein Apr 02 '20 at 18:29
  • On my answer there's no way the function will return null, there is only true or false, what does it return? – Michel Feinstein Apr 02 '20 at 18:30
  • Your question doesn't match your code, you ask about returning a value, but in your code you use `setState` and return, I am answering your question about return. Also, why do you even set it to true and false? – Michel Feinstein Apr 02 '20 at 18:38
  • This functions role is to check whether a user exists in the collection if he exists then I won't register him/her. If it doesn't exist then I need to store his/her details in the collection – Pranav Arora Apr 02 '20 at 18:42
  • Ok, but your question is not about any of this. You are asking why the function returns null, that's all that matters here. It returns null because you are returning a null variable that hasn't been initialized. My answer provides code that doesn't return null, what you do with the result its up to you. – Michel Feinstein Apr 02 '20 at 18:46
  • It is not the case it is returning the same values for both the cases even if the number is already present in the collection or not – Pranav Arora Apr 02 '20 at 18:56
  • Still it is returning true for all the cases. – Pranav Arora Apr 02 '20 at 19:03
  • So the returned query result is an empty collection (debug to check) or `if (f.data['userid'] == mobileno)` is never true. – Michel Feinstein Apr 02 '20 at 19:07
  • The collection is not empty it consists of 20 odd records and they consist of the field we are comparing. So not able to figure out why this is happening. – Pranav Arora Apr 02 '20 at 19:16
  • Did you print the entire collection? Can you see the records and the data inside them? Is mobileno a String? Declare it as a String as well on the function declaration. – Michel Feinstein Apr 02 '20 at 19:29
  • yes I printed the entire collection. I can see the records and yes userid is a string to which we are comparing the mobile no. – Pranav Arora Apr 03 '20 at 13:20
0

For your case, you can try to use a query that only returns values that are not null. This way, in case it's not null, it will return true and if it's null, it will return false.

One example of how to achieve that - as better explained in this other question from the Community here - you can use the function hasAll(). This function returns only if all values are present. So, you would use it to load all the values to array, verifying this way, if they are not null - something like the below example.

!resource.data.keys().hasAll(['mobileno'])

There are other examples, such as using a String to compare values using the where() function, so you won't get the null values. More information on doing queries are available in this link.

I would recommend you to take a look at them and give these above options a try, to verify if they will help you. :)

Let me know if the information helped you!

gso_gabriel
  • 4,199
  • 1
  • 10
  • 22