isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.01k stars 398 forks source link

flutter: Cannot copy hive dile to storage by file.copy #747

Open tazik561 opened 3 years ago

tazik561 commented 3 years ago

I am trying to copy hive box to my mobile storage. After acception sufficient permissions, I run my method:

  Future<void> exportVocabsDb() async {
    final dir = await getExternalStorageDirectory();
    final evbDirectory =
        await Directory('${dir!.path}/evb').create(recursive: true);
    hiveService.backupHiveBox<VocabularyModel>(
        VOCABULARY_BOX, evbDirectory.path);
  }

and :

  Future<void> backupHiveBox<T>(String boxName, String backupPath) async {
    if (!Hive.isBoxOpen(boxName)) {}
    final box = await Hive.openBox<T>(boxName);
    final boxPath = box.path;
    await box.close();

        try {
  File(boxPath!).copy(backupPath);
  } finally {
  await Hive.openBox<T>(boxName);
  }
  }

But I got this error:

E/flutter (14955): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot copy file to '/storage/emulated/0/Android/data/com.example.english_vocabulary_builder/files/evb', path = '/data/user/0/com.example.english_vocabulary_builder/app_flutter/vocabularybox.hive' (OS Error: Is a directory, errno = 21)

There is hive file inside app_flutter :

https://pasteboard.co/KdI1Juxp.png

this is doctor:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on Linux, locale en_US.UTF-8)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/linux#android-setup for more details.
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] Android Studio
[✓] VS Code (version 1.58.2)
[✓] Connected device (2 available)

! Doctor found issues in 1 category.
ghost commented 3 years ago

Hey @tazik561, looks like you are trying to copy a file object and copying it into a folder object. Trying correcting

    final evbDirectory = await Directory('${dir!.path}/evb').create(recursive: true);

to

    final evbDirectory = await File('${dir!.path}/evb').create(recursive: true);