After get() of DocumentSnapshot, program is not executing next line in Flutter

Viewed 28

Function1 and Function2 are not executed completely.

//Function1

Future<void> getUserDetails() async {
  
    DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance
        .collection('users')
        .doc('aLhON9H3H1ZpZvUB4ISUrK45Hk93')
        .get();
        if (documentSnapshot.exists) {
            globalImagePath = documentSnapshot['imageURL'];
      }
  }

//Function2 (fetch data from subcollection of "users" collection

Future<void> fetchPunchinDetails() async {
  
    try {
      var result = await _users
          .doc(user!.uid)
          .collection('attendance')
          .where('outTime', isEqualTo: null)
          .get();

     String dayStart = result.docs[0]['inTime'];
      if (dayStart.isNotEmpty) {
        dayStarted = true;
      }
    } catch (e) {
      print(e.toString());
    }
  }

// Calling above two methods

_fetchAndGotoScreens() {

    if (loginSuccess = true) {
      getUserDetails();   //--------calling Function1
      fetchPunchinDetails();  //----------calling Function2

     
        //Go to attendance page-----------
        Navigator.of(context)
            .push(
          MaterialPageRoute(
            builder: (context) =>
                Attendance(imagePath: imagePath, dayStartTime: dayStartTime),
          ),
        )
            .catchError((e) {
          print(e);
        });
      } else {
        // go to home page------------
        Navigator.of(context)
            .push(MaterialPageRoute(builder: (context) => MyHome()))
            .catchError((e) {
          print(e);
        });
      }
  
  }

When I step through, in getUserDetails() itself, it returns to the calling method just after executing .get(). It is not even checking if (documentSnapshot.exists) condition. The same happens with fetchPunchinDetails() function. Please help..

1 Answers

fetchPunchinDetails and getUserDetails are future method, try using await before theses to complete.

_fetchAndGotoScreens() async{

    if (loginSuccess = true) {
     await getUserDetails();   
     await fetchPunchinDetails();   

Related