英文:
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<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??[];
}
答案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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论