getIDFromRoot method

Future<String?> getIDFromRoot(
  1. String filename, {
  2. String? mimeType,
  3. bool createIfMissing = false,
})

Implementation

Future<String?> getIDFromRoot(String filename, {String? mimeType, bool createIfMissing = false}) async {
  if(!await ready()) return null;
  try{
    if(filename == "" || filename == "/") return scope == DriveApi.driveAppdataScope ? "appDataFolder" : "root";
    var parentID = scope == DriveApi.driveAppdataScope ? "appDataFolder" : "root";
    var split = filename.split("/");
    List<File>? out;
    for(int i = 0; i< split.length; i++){
      var fold = split[i];
      if(fold == "") continue;
      var query = DriveQueryBuilder();
      if(i != split.length -1){
        query.mime = DriveQueryBuilder.folderMime;
      }else{
        query.mime = mimeType;
      }
      query.name = fold;
      query.parent = parentID;
      out = (await api!.files.list(
        spaces: (scope == DriveApi.driveAppdataScope) ? "appDataFolder" : "drive",
        q: query.getQuery()
      )).files;
      if (out == null || out.isEmpty) {
        if (!createIfMissing) return null;
        var id = await createFileWithParent(fold, parentID, mimeType: query.mime);
        if (id == null) return null;
        parentID = id;
        var fil = await getFile(id);
        if(fil == null) return null;
        out = [fil];
        continue;
      }
      if(out[0].id == null) {
        if (!createIfMissing) return null;
        var id = await createFileWithParent(fold, parentID, mimeType: query.mime);
        if (id == null) return null;
        parentID = id;
        var fil = await getFile(id);
        if(fil == null) return null;
        out = [fil];
        continue;
      }
      parentID = out[0].id!;
    }
    return out![0].id!;
  }catch(e, stack){
    if(e is PlatformException && e.code == "network_error"){
      return null;
    }
    if(kDebugMode){
      print("getIDFromRoot:");
      print("${e.toString()}\n${stack.toString()}");
    }else if (onError != null){
      onError!(e, stack);
    }
    return null;
  }
}