androidflutterdartandroid-manifestsqflite

Sqflite data is lost after app update/upgrade


I'm building an app that uses sqflite^2.0.2 to store data as a local database.

Everything works fine, but the problem arises when updating the application on the device: all the data is lost. It seems like the database gets deleted once the app it's upgraded or reinstalled. The behaviour it's the same on an Android emulator.

I've already tried adding android:allowBackup="true" in the AndroidManifest.xml, and it's still not working even if - as documentation states - should backup the databases.

Since the database structure it's not being updated, but only the actual Flutter application is, the Database version should remain 1 (?).

static Future<Database> init() async {
  WidgetsFlutterBinding.ensureInitialized();

  final database = openDatabase(
    join(await getDatabasesPath(), 'myDatabase.db'),
    onCreate: (db, version) {
      return db.execute(
        'CREATE TABLE myOrders (id TEXT PRIMARY KEY, name TEXT, quant INTEGER)',
      );
    },
    version: 1, 
  );
  return database;
}

The code above initializes my DB, and the join() returns the follwing path: /data/user/0/com.example.myapp/databases/myDatabase.db

Unfortunately, this didn't help: https://stackoverflow.com/a/62845264/11442598

Am I missing something? Any help is appreciated.


Solution

  • Problem solved.

    At first, I tried using path_provider to be sure the right path was being used everytime for the database. That didn't work, but it was still a good practice for iOS as recommended by sqflite.

    The next step was to rename the database to something else, maybe there was more that one instance of a DB (?).

    Then I've deleted and downloaded again my Android Emulator, choosing one that had Google Play installed.

    At last, I added the

    <application android:allowBackup="true"/>
    

    string into the AndroidManifest.xml


    I ran a flutter clean and flutter pub get between all these actions.

    After a few tries, the data was not lost anymore.