Flutter: 如何从Google Drive API中获取特定扩展名的文件并将其作为列表返回?

huangapple go评论68阅读模式
英文:

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

问题

我允许用户将备份文件保存到他/她的Google Drive,并将其保存到应用程序的文件夹中。我还获取了该备份文件的列表到应用程序,到目前为止一切都正常,但是如果用户从应用程序外部上传任何文件到该文件夹,例如图片,那么如果用户点击恢复按钮,应用程序会出现错误,我只想检索.db文件而已?

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 ?? [];
}
英文:

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&lt;List&lt;drive.File&gt;&gt; _filesInFolder(drive.DriveApi driveApi) async {
    var res = await driveApi.files.list(
      spaces: &#39;drive&#39;,
      q: &quot;&#39;$folderId&#39; in parents and trashed=false&quot;,
    );
    return res.files??[];
  }

答案1

得分: 0

你需要将代码更改为:

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!.where(
      (element) => element.name!.endsWith(dbExtension),
    );

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

我不知道你是如何调用它的,因为你没有给我们足够的代码,但我会假设你将它添加到一个列表中,所以你应该添加:

List<drive.File> _files = [];

然后通过调用上面的函数来获取这些文件:

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

然后你可以通过FutureBuilder小部件来调用_fetchFiles

英文:

You need to change the code as:

 Future&lt;Iterable&lt;drive.File&gt;&gt; _filesInFolder(drive.DriveApi driveApi) async {
    const dbExtension = &#39;.db&#39;;
    var res = await driveApi.files.list(
      spaces: &#39;drive&#39;,
      q: &quot;&#39;$folderId&#39; in parents and trashed=false&quot;,
    );
    final file = res.files!.takeWhile(
      (element) =&gt; 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&lt;drive.File&gt; _files = [];

And then fetch that files via calling the above function:

  Future&lt;void&gt; _fetchFiles() async {
    try {
      var files = await _filesInFolder(driveFile);
      _files = files.toList();
    } catch (error) {
      print(&quot;Error fetching files: $error&quot;);
    }
  }

Then you can call _fetchFiles via FutureBuilder widget.

huangapple
  • 本文由 发表于 2023年6月19日 07:15:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定