Backing up my local database on google drive always creates a different file id

Viewed 646

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?

3 Answers

What you need to first check if the file is present in the drive by the name. Since there is not direct API to fetch a file with a name from google drive, you need to make use of the List api and get the files first before checking their name

For that you can use the following query

{
  q: `'appDataFolder' in parents and trashed = false`
}

Once you get the response you can check if your file is present by name. If its get its id and trigger an update call for the file.

Note: you do not pass parents key to upload but addParents

For media upload you would use the following url

PATCH https://www.googleapis.com/upload/drive/v3/files/fileId

If you do not find the file, you go by the method of creating a new one

// 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()),
);

If you want to set the id of the file you have to use a generated id from google. That's why you're getting the provided file ID is not usable. Theres's a class you can use called generateIds which you can use to create id's that can be used with create requests as you've done above. The Google Drive API developer website has a tool where you can make requests to the api. It's called "Try it now"(like postman) For example, create the list of ids(just press execute) here. Pick one of the ids and add it to the request body (in the request body box on the left side , press the plus sign to get the id key and add the generatedId) here . You should get a 200 response with the id you sent with the request. It will also return a specific error message that you can handle if the id already exists (code 409)

you don't have to specify the

fileToUpload.parents = ["appDataFolder"];

while updating a file in google drive

Related