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


This Content is from Stack Overflow. Question asked by user19976096

Function1 and Function2 are not executed completely.


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

//Function2 (fetch data from subcollection of “users” collection

Future<void> fetchPunchinDetails() async {
    try {
      var result = await _users
          .where('outTime', isEqualTo: null)

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

// Calling above two methods

_fetchAndGotoScreens() {

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

        //Go to attendance page-----------
            builder: (context) =>
                Attendance(imagePath: imagePath, dayStartTime: dayStartTime),
            .catchError((e) {
      } else {
        // go to home page------------
            .push(MaterialPageRoute(builder: (context) => MyHome()))
            .catchError((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..


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

_fetchAndGotoScreens() async{

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

This Question was asked in StackOverflow by user19976096 and Answered by Yeasin Sheikh It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?