0

my recursive function to get all .jpg files from device

Directory dir = Directory("/storage/emulated/0");
      late CameraController controller;
      List<String> visited = [];
      List<String> files = [];
    
     Future getphotos(olddir) async {
        Directory newdir = olddir;
    
        for (var item in newdir.listSync()) {
          if (item.runtimeType.toString() == "_Directory" &&
              !visited.contains(item.toString()) &&
              !item.path.contains(".thumbnail")) {
            visited.add(item.toString());
            await getphotos(item);
          } else {
            if (item.path.endsWith(".jpg")) {
              files.add(item.path);
            }
          }
        }

my future builder

FutureBuilder<Object>(
                future: temp(),
                builder: (context, snapshot) {
                  return GridView.builder(
                      scrollDirection: Axis.horizontal,
                      shrinkWrap: true,
                      gridDelegate:
                          const SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 5),
                      //if file/folder list is grabbed, then show here
                      itemCount: snapshot.hasData ? files.length : 5,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(3.0),
                          child: !snapshot.hasData
                              ? Image.asset("assets/images/load.gif")
                              : Image.file(
                                  File(files[index]),
                                  fit: BoxFit.cover,
                                  filterQuality: FilterQuality.low,
                                  width: 200,
                                  height: 200,
                                ),
                        );
                      });
                });

problem: my futurebuilder build its child widget only when the above recursive function is finished, screen stuck while its loading all the images path

nik nikhil
  • 11
  • 1
  • look at this answer here: https://stackoverflow.com/questions/41375905/await-inside-for-loop-is-admitted-in-dart use the second type of for loop and see if it works – Benyamin Nov 18 '21 at 18:23
  • Maybe you want to post a progress indicator while the recursive function is running. Look at [this answer](https://stackoverflow.com/questions/58363902/flutter-futurebuilder-show-progress-indicator?rq=1) for ideas – Curt Eckhart Nov 18 '21 at 19:44

0 Answers0