Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: How to get spcific extention file from google drive api as a list?

I let the user to save a backup file to his/her google drive, and save it to app’s folder. I also get a list of that backup files to the app until here every thing works fine, but if the user upload any file to that folder from outside the app e.g an image, so if the user hit the button recover, the app run an error, i just want to retrive the .db files only?

  Future<List<drive.File>> _filesInFolder(drive.DriveApi driveApi) async {
    var res = await driveApi.files.list(
      spaces: 'drive',
      q: "'$folderId' in parents and trashed=false",
    );
    return res.files??[];
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You need to change the code as:

 Future<Iterable<drive.File>> _filesInFolder(drive.DriveApi driveApi) async {
    const dbExtension = '.db';
    var res = await driveApi.files.list(
      spaces: 'drive',
      q: "'$folderId' in parents and trashed=false",
    );
    final file = res.files!.takeWhile(
      (element) => element.name!.endsWith(dbExtension),
    );

    if (file.isEmpty) {
      return [];
    } else {
      return file;
    }
  }

I don’t know how did you call it since you did not give us an enough code but I will imaging that you are adding it to a list so you should add also :

  List<drive.File> _files = [];

And then fetch that files via calling the above function:

  Future<void> _fetchFiles() async {
    try {
      var files = await _filesInFolder(driveFile);
      _files = files.toList();
    } catch (error) {
      print("Error fetching files: $error");
    }
  }

Then you can call _fetchFiles via FutureBuilder widget.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading