flutter - 在谷歌驱动器上备份我的本地数据库总是会创建一个不同的文件 ID
问题描述
我正在用颤振创建一个待办事项列表应用程序,我希望我的用户能够在谷歌驱动器上备份他们的任务。这是我正在使用的代码:
// Create the file we want to upload.
ga.File fileToUpload = ga.File();
var file = await _localFile;
fileToUpload.parents = ["appDataFolder"];
fileToUpload.name = path.basename(file.absolute.path);
// Create a new back-up file on google drive.
var response = await drive.files.create(
fileToUpload,
uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
);
// Get the file id.
fileId = response.id;
问题是,每次我得到不同的文件 ID 时,我都需要从谷歌驱动器中检索具有相同文件 ID 的文件,而不是每次都使用不同的 ID。
我尝试使用 update 方法而不是 create 方法:
ga.File fileToUpload = ga.File();
var file = await _localFile;
fileToUpload.parents = ["appDataFolder"];
fileToUpload.name = path.basename(file.absolute.path);
drive.files.update(fileToUpload, fileId);
但我得到未处理的异常:DetailedApiRequestError(状态:403,消息:父字段在更新请求中不可直接写入。请改用 addParents 和 removeParents 参数。)
我还尝试在使用 create 方法之前设置文件 ID:
fileToUpload.id = fileId;
await drive.files.create(
fileToUpload,
uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
);
但后来我得到未处理的异常:DetailedApiRequestError(状态:400,消息:提供的文件 ID 不可用。)或者具有该 ID 的文件已经存在。
所以我试图从谷歌驱动器中删除该文件,然后使用相同的 ID 再次创建它:
fileToUpload.id = fileId;
drive.files.get(fileId).then((value) {
if (value != null) {
drive.files.delete(fileId).then((value) {
drive.files.create(
fileToUpload,
uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
);
});
} else {
drive.files.create(
fileToUpload,
uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
);
}
});
但后来我也得到未处理的异常:DetailedApiRequestError(状态:400,消息:提供的文件 ID 不可用。)即使我使用谷歌驱动器为原始文件提供的相同文件 ID。
有什么解决办法吗?
解决方案
如果要设置文件的 id,则必须使用 google 生成的 id。这就是为什么你得到the provided file ID is not usable
. 您可以使用一个名为generateIds的类,您可以使用它来创建可与创建请求一起使用的 id,就像您在上面所做的那样。Google Drive API 开发者网站有一个工具,您可以在其中向 api 发出请求。它被称为“立即尝试”(如邮递员)例如,在此处创建 id 列表(只需按执行) 。选择其中一个 id 并添加到请求正文中(在左侧的请求正文框中,按加号获取id
密钥并添加生成的Id)这里. 您应该收到 200 响应,其中包含您随请求发送的 id。如果 id 已存在(代码 409),它还将返回您可以处理的特定错误消息
推荐阅读
- c++ - 线程死锁(我认为)
- java - 膨胀类android.fragment.app.FragmentContainerView时出错,有人可以告诉我我缺少什么
- r - 为什么会有空白?
- ios - 为什么 Swift 在调试模式下这么慢
- node.js - 如何使用 expressJS 获取完整的动态 URL
- python - 如何配置我的数据包 Scapy 的协议层?
- java - 使用 maven 将 dll 添加到 java.library.path
- python-3.x - 没有json的谷歌cloude认证
- shinyproxy - shinyproxy 无法使用默认用户和密码登录
- android - 在 Flutter FireBase 谷歌签名中,如何为我的给定代码维护持久性签名