Future Builder is Not Building

Issue

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

Solution

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'
}

Answered By – Mehmet Ali Bayram

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published