adamcapjones
adamcapjones

Reputation: 193

Future Builder is Not Building

I am trying to log a user in with the boolean value assigned to the 'isVerified' field in the user's firestore document.

In other words, If 'isVerified' is true then continue, else return to verify page.

I put in debugPrint statements to help me catch the error and it appears that the Future Builder is not getting past the builder context. I have read other documentation to regarding future builders but I can't find where I'm going wrong, please let me know if there's anything I can clarify. Thank you

Using Future Builder for async

FutureBuilder (
        future: getVerified(),
        builder: (context, snapshot) { <--------- Nothing past this line is running
          debugPrint('>> Home: FutureBuilder: checkpoint'); // does not print to console
          if (snapshot.hasData && !snapshot.hasError) {
            debugPrint('>> Home: FutureBuilder: Snapshot has data and no error');
          }
          return const Text('');
        }
      );

Future

Future<bool> getVerified() async {
  debugPrint('>> Home: getVerified Started');
  User? user = auth.currentUser;
  await FirebaseFirestore.instance
      .collection('users')
      .doc(user!.uid)
      .get()
      .then((value) {
    bool isVerified = value.data()!['isVerified'];
    debugPrint('>> Home: getVerified $isVerified'); // this variable is currently true or false
    return isVerified; // this will return Instance of '_Future'
  });
  return false;
}

Upvotes: 3

Views: 786

Answers (4)

Ali Bayram
Ali Bayram

Reputation: 7921

You don't need to change FutureBuilder it is good. And I recode your getVerified() function.

Can you try

Future<bool> getVerified() async {
  debugPrint('>> Home: getVerified Started');
  bool isVerified = false; // set your response to false

  // get your user
  final user = FirebaseAuth.instance.currentUser;

  // check the data from firestore if the user is not null
  if (user != null) {
    final docSnapShot = await FirebaseFirestore.instance
        .collection('users')
        .doc(user.uid)
        .get();

    if (docSnapShot.exists) {
      isVerified = docSnapShot.data()!['isVerified'];
    }
  }

  debugPrint(
      '>> Home: getVerified $isVerified'); // this variable is currently true or false
  return isVerified; // this will return Instance of '_Future'
}

Upvotes: 1

Rutvik Moradiya
Rutvik Moradiya

Reputation: 88

you are using function that returns bool value, FutureBuilder future cannot able to get the data. Try to donot use bool.

Future<bool> getVerified() async {
      debugPrint('>> Home: getVerified Started');
      User? user = auth.currentUser;
      await FirebaseFirestore.instance
          .collection('users')
          .doc(user!.uid)
          .get()
          .then((value) {
        bool isVerified = value.data()!['isVerified'];
        debugPrint('>> Home: getVerified $isVerified'); // this variable is currently true or false
        return isVerified; // this will return Instance of '_Future'
      });
      return false;
    }

Upvotes: 1

GJJ2019
GJJ2019

Reputation: 5172

try this

Future<bool> getVerified() async {
   debugPrint('>> Home: getVerified Started');
   User? user = auth.currentUser;
  if(user != null) {
  final value = await FirebaseFirestore.instance
    .collection('users')
    .doc(user!.uid)
    .get();

   bool isVerified = value.data()!['isVerified'];
   return isVerified;
  } else {
 return false;
 }
}

don't use promises in future method use async await

Upvotes: 1

Tejas Patel
Tejas Patel

Reputation: 510

FirebaseFirestore.instance.collection('users').doc(user.uid).where('your filed', isEqualTo: 1).get();

Upvotes: 1

Related Questions