0

I am using google drive API to download an excel file in my Flutter app but I want to store the downloaded file content response in a File and then do some update operations using excel dart package, below is the given code from reading an xlsx file from a path location.

    var file = "Path_to_pre_existing_Excel_File/excel_file.xlsx";  //here I want to store the response from drive api
    var bytes = File(file).readAsBytesSync();
    var excel = Excel.decodeBytes(bytes);
    
    //Do some logic here
    for (var table in excel.tables.keys) {
      print(table); //sheet Name
      print(excel.tables[table].maxCols);
      print(excel.tables[table].maxRows);
      for (var row in excel.tables[table].rows) {
        print("$row");
      }
    }

    //then saving the excel file
     
    // updating the excel sheet to Drive
     updateToDrive(excel,fileId);

I have created all the required auth functions, drive scopes and my download function looks like this :

Future<void> downloadFile() async{
 
  String fileId = '1TOa4VKfZBHZe######WLA4M95nOWp';
  final response = await driveApi.files.get(
      fileId,
      downloadOptions: drive.DownloadOptions.fullMedia
  );
  print(response);
    
}

This function is executing correctely and giving Media type response, but I could not able to read this response so that I could store it in a file. Any help would be truly appreciated, Thanks

Madhav mishra
  • 313
  • 4
  • 20
  • Use `path_provider` to find the temporary directory, choose a filename, append that to the path and then `File(tempFileName).writeAsBytesSync(response.bodyBytes);` – Richard Heap Feb 09 '22 at 18:08
  • Thanks you for answering, "response" is an Object so it is giving me this error "The getter 'bodyBytes' isn't defined for the type 'Object'. " I tried to change the type of "response" variable to Media but drive.file.get() returns an object(as it is saying in my errors) – Madhav mishra Feb 09 '22 at 18:19
  • `response` should have a `stream` getter that returns a stream. So `File(name).openWrite()` should give you a sink. Listen to the stream and `add` the bytes to the sink. Close everything on `onDone` - and you can then read the temp file. See: https://stackoverflow.com/questions/49643460/dart-flutter-download-or-read-the-contents-of-a-google-drive-file – Richard Heap Feb 09 '22 at 18:30
  • I couldn't able to make this happen in my code as I'm not aware with getter and stream. drive.files.get() returns Future which I could not able to handle, could you post your answer with full/partial code if possible ? – Madhav mishra Feb 09 '22 at 18:52

1 Answers1

0

I changed my download function to this, as drive.files.get() was returning a Future Object so I changed it to return Future<Media?> by type casting.

String fileId = "19jF3lOVW563LU6m########jXVLNQ7poXY1Z";
  drive.Media? response = (await driveApi.files.get(
      fileId,
      downloadOptions: drive.DownloadOptions.fullMedia
  )) as drive.Media?;

Now response is a Media on which we can listen to the sream to store the response in a file. To do that first we need to get the app directory by path_provider

final String path = (await getApplicationSupportDirectory()).path;
final String fileName = '$path/Output.xlsx';
File file = File(fileName);

Now we want to write the stream of response Stream<List> into our file object which I found from this link

List<int> dataStore = [];
  await response!.stream.listen((data) {
    print("DataReceived: ${data.length}");
    dataStore.insertAll(dataStore.length, data);
  }, onDone: () {
    print("Task Done");
    file.writeAsBytes(dataStore);
    OpenFile.open(file.path);
    print("File saved at ${file.path}");
  }, onError: (error) {
    print("Some Error");
  });

Now we can do whatever we want to make changes through excel package.

Madhav mishra
  • 313
  • 4
  • 20
  • I am facing exactly a similar issue and I tried above mentioned solution but it is not working. I am getting this error - "Exception has occurred. _CastError (type 'File' is not a subtype of type 'Media?' in type cast)". can you share any other solutions if you have or help to fix this exception? – Bhavin Hirpara Apr 19 '22 at 15:10