I am creating a to-do list app with flutter, and I want my users to be able to back-up their tasks on google drive. This is the code I'm using:
// 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;
The problem is that every time I get a different file id and I need to retrieve the file from google drive with the same file id all the time and not with a different id every time.
I've tried using the update method instead of the create method:
ga.File fileToUpload = ga.File();
var file = await _localFile;
fileToUpload.parents = ["appDataFolder"];
fileToUpload.name = path.basename(file.absolute.path);
drive.files.update(fileToUpload, fileId);
But I get Unhandled Exception: DetailedApiRequestError(status: 403, message: The parents field is not directly writable in update requests. Use the addParents and removeParents parameters instead.)
I also tried to set the file id before using the create method:
fileToUpload.id = fileId;
await drive.files.create(
fileToUpload,
uploadMedia: ga.Media(file.openRead(), file.lengthSync()),
);
But then I get Unhandled Exception: DetailedApiRequestError(status: 400, message: The provided file ID is not usable.) Or that a file with that id is already exists.
So I've tried to delete the file from google drive and then create it again with the same 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()),
);
}
});
But then I also get Unhandled Exception: DetailedApiRequestError(status: 400, message: The provided file ID is not usable.) Even though I'm using the same file id given by google drive for the original file.
Any solution?