Dart / flutter: download or read the contents of a Google Drive file
I had quite a bit of trouble with this, it seems much harder than it should be. Also this is for TXT files only. You need to use files.export() for other files.
First you need to get a list fo files.
ga.FileList textFileList = await drive.files.list(q: "'root' in parents");
Then you need to get those files based on ID (This is for TXT Files)
ga.Media response = await drive.files.get(filedId, downloadOptions: ga.DownloadOptions.FullMedia);
Next is the messy part, you need to convert your Media object stream into a File and then read the text from it. ( @Google, please make this easier.)
List<int> dataStore = [];
response.stream.listen((data) {
print("DataReceived: ${data.length}");
dataStore.insertAll(dataStore.length, data);
}, onDone: () async {
Directory tempDir = await getTemporaryDirectory(); //Get temp folder using Path Provider
String tempPath = tempDir.path; //Get path to that location
File file = File('$tempPath/test'); //Create a dummy file
await file.writeAsBytes(dataStore); //Write to that file from the datastore you created from the Media stream
String content = file.readAsStringSync(); // Read String from the file
print(content); //Finally you have your text
print("Task Done");
}, onError: (error) {
print("Some Error");
});
Zoltán Györkei
I work as a test automation engineer, but in my free time I decided to pick up some development skills. My weapon of choice became Dart and the flutter SDK (for starters), and my first project is to create an app for an awesome animal shelter who rescue sledge dogs.
Updated on December 04, 2022Comments
-
Zoltán Györkei over 1 year
I have a public (anyone with the link can view) file on my Google Drive and I want to use the content of it in my Android app.
From what I could gather so far, I need the fileID, the OAuth token and the client ID - these I already got. But I can't figure out what is the exact methodology of authorising the app or fetching the file.
EDIT:
Simply reading it using file.readAsLines didn't work:
final file = new File(dogListTxt); Future<List<String>> dogLinks = file.readAsLines(); return dogLinks;
The dogLinks variable isn't filled with any data, but I get no error messages.
The other method I tried was following this example but this is a web based application with explicit authorization request (and for some reason I was never able to import the dart:html library).
The best solution would be if it could be done seamlessly, as I would store the content in a List at the application launch, and re-read on manual refresh button press.
I found several old solutions here, but the methods described in those doesn't seem to work anymore (from 4-5 years ago).
Is there a good step-by-step tutorial about integrating the Drive API in a flutter application written in dart?
-
Günter Zöchbauer about 6 yearsPlease provide concrete information about what you tried and what didn't work with these attempts (code, and error messages)
-
Zoltán Györkei about 6 yearsPlease see the EDIT part above: this is already a new approach, which at least builds, but doesn't give any error messages or result either.
-
Zoltán Györkei about 6 yearsIt seems I did get some error after all, and this method can't be used to open remote files: FileSystemException: Cannot open file, path = 'drive.google.com/file/d/1UZWDwA2SXnizz__ZodlLp82MQCi1WnvM' (OS Error: No such file or directory, errno = 2)
-
Günter Zöchbauer about 6 yearsYou can't use
dart:html
in Flutter.dart:html
can only be used in browser apps. stackoverflow.com/questions/48477625/… might help with pub.dartlang.org/packages/googleapis
-
-
Madhav Mishra about 2 yearsDo you have any other way to get the response because I'm using driveApi v3 version and this line does not work "ga.Media response = await drive.files.get(filedId, downloadOptions: ga.DownloadOptions.FullMedia);" because drive.files.get() returns an Future<Object> which could not be receive in a ga.Media type. Is there any other way around this. Anyhelp would be appreciated, thanks
-
Madhav Mishra about 2 yearsHow could we use CustomDriveApi. Could you explain it more ?Thanks