Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 786
adamcapjones Avatar asked Dec 06 '25 02:12

adamcapjones


2 Answers

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'
}
like image 187
Mehmet Ali Bayram Avatar answered Dec 07 '25 17:12

Mehmet Ali Bayram


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

like image 23
Tejas Patel Avatar answered Dec 07 '25 18:12

Tejas Patel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!