Google Drive Integration In Flutter (Upload, Download And List Files)

Introduction

 
In this article we will learn how to integrate Google Drive in the Flutter app. We will learn how we can upload, list and download files to Google drive using Flutter. I have used Google Plus login for authentication and then accessed Google drive. I have studied the Google Drive API but found this way is better and native at the end. So let’s start our Google Drive implementation in Flutter.
 
Output
 
Google Drive Integration In Flutter (Upload, Download And List Files)
 

Steps

 
Step 1
 
The first and most basic step is to create new application in Flutter. If you are a beginner then you can check my blog Create a first app in Flutter. I have created an app named “flutter_gdrive”.
 
Step 2
 
Integrate Google Plus Login into your project. I have written an article on this. Please check it out.
 
Step 3
 
Now, as you have already created a Google Firebase Project, now it’s time to enable Google Drive API from Google Developer Console. Note that you need to select the same project in Google Developer Console as you have created in Google Firebase. In my case it is “Flutter Gdrive”. Check the below screenshots to understand it more. 
 
Google Drive Integration In Flutter (Upload, Download And List Files)
 
Google Drive Integration In Flutter (Upload, Download And List Files)
 
Step 4
 
Now, we will add the dependencies for implementing Google Drive Operations (Google API, Pick File to Upload & Download file in mobile storage). Please check below dependencies.
  1. dependencies:  
  2.  flutter:  
  3.    sdk: flutter  
  4.  cupertino_icons: ^0.1.2  
  5.  firebase_auth: ^0.15.2  
  6.  google_sign_in: ^4.1.0  
  7.  flutter_secure_storage: ^3.3.1+1  
  8.  googleapis: ^0.54.0  
  9.  googleapis_auth: ^0.2.11  
  10.  path_provider: ^1.5.1  
  11.  file_picker: ^1.3.8  
Step 5
 
We need to define permission for file reading and writing. For that define the following permissions in AndroidManifest File. (android/app/src/main/AndroidManifest.xml)
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  2. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
Step 6
 
Now, we will implement File Upload, Listing and Download Login programmatically in main.dart file. Following is the class for mapping Google Auth Credential to GoogleHttpClient Credentials.
  1. class GoogleHttpClient extends IOClient {  
  2.  Map<String, String> _headers;  
  3.  GoogleHttpClient(this._headers) : super();  
  4.  @override  
  5.  Future<http.StreamedResponse> send(http.BaseRequest request) =>  
  6.      super.send(request..headers.addAll(_headers));  
  7.  @override  
  8.  Future<http.Response> head(Object url, {Map<String, String> headers}) =>  
  9.      super.head(url, headers: headers..addAll(_headers));  
  10. }  
Step 7
 
Following is the programming implementation of Google plus login, logout, file upload to Google drive, file download and listing of uploaded files on Google drive.
Google Login:
  1. Future<void> _loginWithGoogle() async {  
  2.    signedIn = await storage.read(key: "signedIn") == "true" ? true : false;  
  3.    googleSignIn.onCurrentUserChanged  
  4.        .listen((GoogleSignInAccount googleSignInAccount) async {  
  5.      if (googleSignInAccount != null) {  
  6.        _afterGoogleLogin(googleSignInAccount);  
  7.      }  
  8.    });  
  9.    if (signedIn) {  
  10.      try {  
  11.        googleSignIn.signInSilently().whenComplete(() => () {});  
  12.      } catch (e) {  
  13.        storage.write(key: "signedIn", value: "false").then((value) {  
  14.          setState(() {  
  15.            signedIn = false;  
  16.          });  
  17.        });  
  18.      }  
  19.    } else {  
  20.      final GoogleSignInAccount googleSignInAccount =  
  21.          await googleSignIn.signIn();  
  22.      _afterGoogleLogin(googleSignInAccount);  
  23.    }  
  24.  }  
  25.    
  26.  Future<void> _afterGoogleLogin(GoogleSignInAccount gSA) async {  
  27.    googleSignInAccount = gSA;  
  28.    final GoogleSignInAuthentication googleSignInAuthentication =  
  29.        await googleSignInAccount.authentication;  
  30.    
  31.    final AuthCredential credential = GoogleAuthProvider.getCredential(  
  32.      accessToken: googleSignInAuthentication.accessToken,  
  33.      idToken: googleSignInAuthentication.idToken,  
  34.    );  
  35.    
  36.    final AuthResult authResult = await _auth.signInWithCredential(credential);  
  37.    final FirebaseUser user = authResult.user;  
  38.    
  39.    assert(!user.isAnonymous);  
  40.    assert(await user.getIdToken() != null);  
  41.    
  42.    final FirebaseUser currentUser = await _auth.currentUser();  
  43.    assert(user.uid == currentUser.uid);  
  44.    
  45.    print('signInWithGoogle succeeded: $user');  
  46.    
  47.    storage.write(key: "signedIn", value: "true").then((value) {  
  48.      setState(() {  
  49.        signedIn = true;  
  50.      });  
  51.    });  
  52.  }  
Google Logout
  1. void _logoutFromGoogle() async {  
  2.    googleSignIn.signOut().then((value) {  
  3.      print("User Sign Out");  
  4.      storage.write(key: "signedIn", value: "false").then((value) {  
  5.        setState(() {  
  6.          signedIn = false;  
  7.        });  
  8.      });  
  9.    });  
  10.  }  
Upload File to Google Drive
  1. _uploadFileToGoogleDrive() async {  
  2.    var client = GoogleHttpClient(await googleSignInAccount.authHeaders);  
  3.    var drive = ga.DriveApi(client);  
  4.    ga.File fileToUpload = ga.File();  
  5.    var file = await FilePicker.getFile();  
  6.    fileToUpload.parents = ["appDataFolder"];  
  7.    fileToUpload.name = path.basename(file.absolute.path);  
  8.    var response = await drive.files.create(  
  9.      fileToUpload,  
  10.      uploadMedia: ga.Media(file.openRead(), file.lengthSync()),  
  11.    );  
  12.    print(response);  
  13.  }  
List Uploaded Files to Google Drive
  1. Future<void> _listGoogleDriveFiles() async {  
  2.    var client = GoogleHttpClient(await googleSignInAccount.authHeaders);  
  3.    var drive = ga.DriveApi(client);  
  4.    drive.files.list(spaces: 'appDataFolder').then((value) {  
  5.      setState(() {  
  6.        list = value;  
  7.      });  
  8.      for (var i = 0; i < list.files.length; i++) {  
  9.        print("Id: ${list.files[i].id} File Name:${list.files[i].name}");  
  10.      }  
  11.    });  
  12.  }  
Download Google Drive File
  1. Future<void> _downloadGoogleDriveFile(String fName, String gdID) async {  
  2.    var client = GoogleHttpClient(await googleSignInAccount.authHeaders);  
  3.    var drive = ga.DriveApi(client);  
  4.    ga.Media file = await drive.files  
  5.        .get(gdID, downloadOptions: ga.DownloadOptions.FullMedia);  
  6.    print(file.stream);  
  7.    
  8.    final directory = await getExternalStorageDirectory();  
  9.    print(directory.path);  
  10.    final saveFile = File('${directory.path}/${new DateTime.now().millisecondsSinceEpoch}$fName');  
  11.    List<int> dataStore = [];  
  12.    file.stream.listen((data) {  
  13.      print("DataReceived: ${data.length}");  
  14.      dataStore.insertAll(dataStore.length, data);  
  15.    }, onDone: () {  
  16.      print("Task Done");  
  17.      saveFile.writeAsBytes(dataStore);  
  18.      print("File saved at ${saveFile.path}");  
  19.    }, onError: (error) {  
  20.      print("Some Error");  
  21.    });  
  22.  }  
Step 8
 
Great -- we are done with Google Drive Integration in Flutter. Please check my full source code for it on Git.
 
 In above source code google-services.json file is not included, you need to setup your own firebase project and put in android->app folder.
 

Conclusion

 
We have learned how to integrate Google Drive in Flutter and perform upload, download and list operations.