How to create folder using google drive api in a flutter app?

1,639

As you have already done Auth credentials, you can use this Simple function to create folder in Google Drive.

clientViaServiceAccount(credentials, scopes).then((http_client) async {
              var _drive = v3.DriveApi(http_client);
              var _createFolder = await _drive.files.create(
                v3.File()
                  ..name = 'FileName'
                  ..parents = ['1f4tjhpBJwF5t6FpYvufTljk8Gapbwajc'] // Optional if you want to create subfolder
                  ..mimeType = 'application/vnd.google-apps.folder',  // this defines its folder
              );
            });
Share:
1,639
Antoun
Author by

Antoun

Updated on December 12, 2022

Comments

  • Antoun
    Antoun over 1 year

    I have tried to create folder in a flutter app in a google drive account. It shows me a bad argument(s) error. Although i am working with the documentation examples

    Here is the code i have tried to do. it generates

    "Bad argument(s)" error.

    final _SCOPES = [drive.DriveApi.DriveScope];
    
      void adrive(){
    
    clientViaServiceAccount(_credentials,_SCOPES).then(onValue).catchError((e)=>print(e));
      }
    
    
      FutureOr onValue(AuthClient client)  {
        //drive.DriveApi(client).files.list().then((value)=>print(value.items[0]));
        // The previous line works fine
        drive.File r = new drive.File();
        r.mimeType = "application/vnd.google-apps.folder";
        r.title = "hello";
    
        drive.DriveApi(client).files.insert(r,uploadOptions: commons.UploadOptions.Resumable).then((f)=>print(f.mimeType+" "+f.originalFilename))
              .catchError((ex)=>print(ex.toString()));
    
    
    
    
    
  • sameer kashyap
    sameer kashyap over 3 years
    I'm unable to create a folder and upload a file to it, the above code does not work, can you help me? I get an Exception telling me the file is not found, which is specified in the parents property