How to backup user data on Google drive and Dropbox for flutter apps?

3,093

It's a very simple way to google, but I haven't dealt with the Drop box

Note : Here is a simple example to illustrate the method

Follow these steps:

1-Add Dependencies :

dependencies:
  googleapis: ^0.56.1
  google_sign_in: ^4.5.6

2-Add these imports :

import 'package:googleapis/drive/v3.dart' as drive;
import 'package:google_sign_in/google_sign_in.dart' as signIn;

3-Google SignIn

Open main.dart, and locate the _incrementCounter() function, which is invoked when the user taps on the plus button , and edit like this :

Future<void> _incrementCounter() async {
  setState(() {
    _counter++;
  });

  final googleSignIn = signIn.GoogleSignIn.standard(scopes: [drive.DriveApi.DriveScope]);
  final signIn.GoogleSignInAccount account = await googleSignIn.signIn();
  print("User account $account");
} 

4-Create a New Firebase Project :

You can check this to create project , and Enabled Auth Google .

5-Enable Google Drive API :

You can also go to Google Cloud Console, Look for the search bar and search for “Google Drive API”. Click “ENABLE”.

6-Upload File to Google Drive :

Create a new file, GoogleAuthClient.dart, using the http package. The GoogleAuthClient class is based on the BaseCleint, which already supports all the standard HTTP requests: POST, GET, etc. We wrap another Client inside and implement the send function where we will inject authentication header later.

import 'package:http/http.dart' as http;

class GoogleAuthClient extends http.BaseClient {
  final Map<String, String> _headers;

  final http.Client _client = new http.Client();

  GoogleAuthClient(this._headers);

  Future<http.StreamedResponse> send(http.BaseRequest request) {
    return _client.send(request..headers.addAll(_headers));
  }
}

Now we create a GoogleAuthClient with the auth headers from the user account above, and then use that to create a Google Drive API object DriveApi.

final authHeaders = await account.authHeaders;
final authenticateClient = GoogleAuthClient(authHeaders);
final driveApi = drive.DriveApi(authenticateClient);

Finally, let’s upload a file :

final Stream<List<int>> mediaStream =
    Future.value([104, 105]).asStream().asBroadcastStream();
var media = new drive.Media(mediaStream, 2);
var driveFile = new drive.File();
driveFile.name = "hello_world.txt";
final result = await driveApi.files.create(driveFile, uploadMedia: media);
print("Upload result: $result");
Share:
3,093
Salman Aljabri
Author by

Salman Aljabri

Updated on December 25, 2022

Comments

  • Salman Aljabri
    Salman Aljabri over 1 year

    How to backup data on user google drive account and dropbox and restore them after reinstall or install the app on different device?

    I have seen apps that allow users to login through google drive and do periodic backup to the user cloud.

    The user data contains images files, Sql database and shared preferences.

    I don't want to use firebase cloud since the data is access by the user himself and not other users and it will be costly if the data is large. Is there any available package can do this? or tutorial otherwise how to achieve this in flutter?