flutterdartgoogle-apigoogle-drive-apidrive

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


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





Solution

  • 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
                  );
                });