android – How to restore an application's database. (Flutter with SQLite)-ThrowExceptions

Exception or error:

I need to develop the functionality to restore the database through a backup file, so the user can recover the data after reinstalling the application.

I am developing the Android application using Flutter and the SQLite device’s own database with lib moor_flutter.
I developed a functionality for the user to back up the database and another to restore the database from that backup file.

The problem is that the restore functionality is not working on some devices.

it didn’t work on: Motorola one zoom / with android 9, redminote 7 / with android 9, redminote 8 / with android 9.

it worked on: Samsung Galaxy S9 / with Android 9, Moto G4, Redmi 4x / with Android 7.1.

Following both functions being used, to create or backup and restore.

  Future<void> backup() async {
    final dbFolder = await getDatabasesPath();
    final dbLocation = p.join(dbFolder, 'dbnew.sqlite');

    final File file = File(dbLocation);
    await ShareExtend.share(file.path, "file");
  }


  Future<void> restore() async {

    final dbFolder = await getDatabasesPath();
    final dbLocation = p.join(dbFolder, 'dbnew.sqlite');
    AppDatabase.instance.close(); 

    //acess file backup
    File file = await FilePicker.getFile(type: FileType.ANY);
    Uint8List updatedContent = await File(file.path).readAsBytes();
    File(dbLocation).writeAsBytes(updatedContent);
  }

Permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *