'Flutter async function with for loop is not returning

I have a really weird behavior right not inside my app and I have absolutely no idea what is wrong with my code. That is my function:

Future<List<UniversityImage>> getImagesForUniversity(
    University university,
  ) async {
    List<UniversityImage> images = [];
    var imagesQuerySnapshot = await firestoreInstance
        .collection(keys.universities)
        .doc(university.id)
        .collection(keys.images)
        .get();

    print(imagesQuerySnapshot.docs.length);   // <-- gets printed

    for (var image in imagesQuerySnapshot.docs) {
      Map<String, dynamic> imageData = image.data();
      images.add(
        UniversityImage(
          id: imageData[keys.id]!,
          index: imageData[keys.index]!,
          universityId: imageData[keys.universityId]!,
          downloadUrl: imageData[keys.downloadUrl]!,
        ),
      );
    }

    print('length:');     // <-- not getting printed
    print(images.length); // <-- not getting printed
    return images;
  }

A simple function that gets some documents from Firebase Firestore. It should add each image to images and in the end I just want to return all the images.

But somehow my code gets stuck inside the for-loop!

What am I missing here? Let me know if you need any more info!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source