0

I have created a function that is implemented in my flutter app, I set up all the configurations in the google cloud console for uploading files to google drive API. My function looks like this

Future<void> uploadFileOnDrive() async {
    try {
      //auth credentials
      final googleSignIn =
          signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
      final signIn.GoogleSignInAccount? account = await googleSignIn.signIn();
      // print("User account $account");
      final authHeaders = await account!.authHeaders;
      final authenticateClient = GoogleAuthClient(authHeaders);
      final driveApi = drive.DriveApi(authenticateClient);
      
      //Uploading the file with "hi" text
      final Stream<List<int>> mediaStream =
          Future.value([104, 105]).asStream().asBroadcastStream();
      var media = new drive.Media(mediaStream, 2);
      var driveFile = new drive.File();
      driveFile.name = "hello_world.txt";
      final result = await driveApi.files.create(driveFile, uploadMedia: media);
      
      //printing the values
      print(result.driveId);
      print(result.name);
      print(result.id);
      print(result.createdTime);
      print("Upload result: $result");
      Fluttertoast.showToast(msg: "Uploaded succesfully");
    } catch (e) {
      Fluttertoast.showToast(msg: e.toString());
    }
  }

This function is uploading files successfully on my drive but I need to edit these files for which I need their ID. The result contains the Instance of 'file' but when I try to print the result.driveId, I get null value but result.name is printing "hello_world.txt" correctly and result.id is also printing some id which is not the actual id of the "hello_world.txt" file (I checked the URL, it does not match). Am I doing anything wrong here? Please correct me. Thanks

Madhav mishra
  • 313
  • 4
  • 20
  • The possible reason why the **driveId** result is `null` is because it is actually the ID of the shared drive/team drive according to this resource [about `driveId`](https://developers.google.com/drive/api/v3/reference/files/list#parameters) which is not the type of Drive location that you're using (you are apparently using your own Google Drive) to save the created `hello_world.txt` file. – SputnikDrunk2 Feb 07 '22 at 16:45
  • Thank you for answering, Is there any way to get the id of the documents for eg. "https://docs.google.com/spreadsheets/d/ID_OF_THE_SPREADSHEET/edit#gid=0" – Madhav mishra Feb 08 '22 at 05:13

0 Answers0