Search code examples
flutterdirectorygoogle-drive-apisubdirectory

Distinguish parent and child folder in Google Drive - Flutter - GoogleAPI V3


I am struggling to find the right folder in Google Drive. I am using import 'package:googleapis/drive/v3.dart'

Folder Structure

--FolderA
 |
 |--FolderC  // Note this sub folder has the same name as one of the other folders
--FolderB
--FolderC
  var query = "mimeType='application/vnd.google-apps.folder' ";
  query += "and name='$folderName'"; // folderName = "FolderC"

....
...

  Future<List<File>> _runQuery(String query) async {
    DriveApi driveApi = DriveApi(await getHttpClient());    
    return (await driveApi.files.list(q: query)).files;
  }

...with the above code I can see the folders with their id, name, etc.. The problem is the parents for all the folders are null. Because of this I coudn't find out which 'FolderC' is which.

So, is there a way to find out a subfolder of a specific folder?

Update 1:

If I add parents in the query, I am getting an empty list (no folders at all)

var query = "mimeType='application/vnd.google-apps.folder' ";
query += "and name='$folderName' "; // folderName = "FolderC"
query += "and '${parentFolder.id}' in parents";

Solution

  • In your case, how about modifying the search query?

    Modified search query:

    "mimeType='application/vnd.google-apps.folder' and name='$folderName' and '### folder ID of FolderA ###' in parents"
    
    • At above search query, FolderC in the folder FolderA is retrieved.

    Note:

    • If you want to retrieve the information of the parent folder at the returned values from the API, please add parents in fields for request. Or you can also use * as the fields. In this case, all metadata can be retrieved.

    References:

    If I misunderstood your question and this was not the result you want, I apologize.