5

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




2 Answers 2

4

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
              );
            });
Sign up to request clarification or add additional context in comments.

1 Comment

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
0

I'm coming late but I hope I'll make some help for others who want to get folder's id, this code I convert it to a flutter from java code from Google official Google Official Workspace.

First you can make a dart class, to authenticate the user:

import 'package:http/http.dart' as http;

class GoogleAuthClient extends http.BaseClient {
  final Map<String, String> _headers;

  final http.Client _client = http.Client();

  GoogleAuthClient(this._headers);

  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) {
    return _client.send(request..headers.addAll(_headers));
  }
}

Then create the folder function:

import 'package:YOUR_APP_NAME/google_auth_client.dart';
import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart' as signIn;


  Future<String?> createFolder() async {
    final googleSignIn =
        signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.driveScope]);
    final signIn.GoogleSignInAccount? account = await googleSignIn.signIn();
    final authheader = await account?.authHeaders;

    var client = GoogleAuthClient(authheader!);
    var driveFile = drive.DriveApi(client);

    // File's metadata.
    var fileMetadata = drive.File();
    fileMetadata.name = 'test';
    fileMetadata.mimeType = 'application/vnd.google-apps.folder';

    try {
      var file = await driveFile.files.create(fileMetadata);
      print('Folder ID: ${file.id}');
      return file.id;
    } catch (e) {
      // TODO: Handle error appropriately
      print('Unable to create folder: $e');
      rethrow;
    }
  }

This will create the folder and gives you the Folder's ID that you will use to upload the file to it.

2 Comments

Hoe to authenticate the user did you use it directly ?
Oh no, to get to google drive authentication I got more steps to follow, I'll try to explain in short, first I add packages as googleapis and google_sing_in then create firebase project and add to it android app so I got need to make SHA1 for firebase, then its automatically created an app in google cloud console with all api keys that we need, also I downloaded the google_service.json from firebase that has all credentials for my app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.