deansyd / icloud_storage

A flutter plugin for uploading and downloading files to and from iCloud
MIT License
42 stars 24 forks source link

DB gets blank while after downloading. #9

Closed aipxperts-developer closed 2 years ago

aipxperts-developer commented 3 years ago

I have configured iCloud as defined in the docs and i already have uploaded 1 file but when i try to download the file using below code DB gets blank (all data cleared).

Future<void> testDownloadFile() async {
    Singleton.instance.buildLoading(context);
    try {
      final iCloudStorage =
          await ICloudStorage.getInstance(Constant.iCloudContainerId);
      StreamSubscription<double> downloadProgressSubcription;
      var isDownloadComplete = false;

      String pathDatabase = await getDatabasesPath();
      String databasePath = path.join(pathDatabase, 'classify.db');
      var file = File(databasePath);

      final directory = await getDatabasesPath();
      print(directory);

      await iCloudStorage.startDownload(
        fileName: 'classifyDB',
        destinationFilePath: file.path,
        onProgress: (stream) {
          stream.listen((event) {});
          downloadProgressSubcription = stream.listen(
            (progress) => print('--- Download File --- progress: $progress'),
            onDone: () {
              isDownloadComplete = true;
              print('--- Download File --- done');
              // Navigator.of(context).pop();
              // GlobalSnackBar.show(context, "Restore is completed.".tr(), false);

              final saveFile = File('$directory/classify.db');
              List<int> dataStore = [];

              saveFile.writeAsBytes(dataStore);
              print("File saved at ${saveFile.path}");

              Navigator.pop(context);
              GlobalSnackBar.show(context, "Restore is completed.".tr(), false);
            },
            onError: (err) {
              print('--- Download File --- error: $err');
              Navigator.of(context).pop();
              GlobalSnackBar.show(
                  context, "Error while restoring Backup.".tr(), false);
            },
            cancelOnError: true,
          );
        },
      );

      Future.delayed(Duration(seconds: 20), () {
        if (!isDownloadComplete) {
          downloadProgressSubcription?.cancel();
          print('--- Download File --- timed out');
          Navigator.of(context).pop();
          GlobalSnackBar.show(context, "Restore is timed out.".tr(), false);
        }
      });
    } catch (err) {
      Navigator.of(context).pop();
      GlobalSnackBar.show(context, "Restore failed.".tr(), false);
      handleError(err);
    }
  }

Can anyone let know what i am missing here? why my DB getting blank?

Thanks