Flutter iOS 应用升级后应用 sqflite 数据库数据丢失

Flutter iOS App sqflite database data lost after app upgrade

我们有一个完全由 flutter 构建的应用程序。当我们将应用程序升级发送到 google 存储时,在 Android 设备中更新 apk 时,sqflite 数据库数据不会丢失。 但是在 iOS 设备中,用户将应用程序更新到新版本后,所有数据库数据都丢失了。

flutter v1.17.5

用于数据持久化的包:

sqflite: ^1.3.1 path_provider: ^1.6.11

你能帮忙解决这个问题吗?应用程序更新到新版本后,不得删除用户数据。也许我需要使用其他类型的数据持久化或者我该如何解决这个问题。

这是我在 DatabaseHelper 中初始化数据库的部分 class:

  class DatabaseHelper {
  static DatabaseHelper _databaseHelper; // Singletone DatabaseHelper
  static Database _database; // Singletone Database

  String wifiSystemTable = 'wifi_system_table';
  String colId = 'id';
  String colDataType = 'data_type';
  String colLocationName = 'location_name';
  String colBackground = 'background';
  String colLocationId = 'location_id';
  String colDeviceType = 'device_type';
  String colIp = 'ip';
  String colName1 = 'name1';
  String colName2 = 'name2';
  String colName3 = 'name3';
  String colRemotePort = 'remote_port';

  DatabaseHelper._createInstance(); // Named constractor to create instance of Database Helper

  factory DatabaseHelper() {
    if (_databaseHelper == null) {
      _databaseHelper = DatabaseHelper
          ._createInstance(); //This is execute only once, singletone object
    }
    return _databaseHelper;
  }

  Future<Database> get database async {
    if (_database == null) {
      _database = await initializeDatabase();
    }
    return _database;
  }

  Future<Database> initializeDatabase() async {
    //Get the directory path for both Android and IOS to store Database
    Directory directory = await getApplicationDocumentsDirectory();
    String path = p.join(directory.toString(), 'wifi.db');

    //Open/create the database at the given path
    var wifiSystemDatabase =
        await openDatabase(path, version: 1, onCreate: _createDb);
    return wifiSystemDatabase;
  }

  void _createDb(Database db, int newVersion) async {
    await db.execute(
        'CREATE TABLE $wifiSystemTable($colId INTEGER PRIMARY KEY, $colDataType INTEGER, $colLocationName TEXT, $colBackground TEXT, $colLocationId INTEGER, $colDeviceType INTEGER, $colIp TEXT, $colName1 TEXT, $colName2 TEXT, $colName3 TEXT, $colRemotePort TEXT)');
  }

非常感谢

我认为您的数据库路径正在更改。试试这个。

  Future<Database> initializeDatabase() async {
    //Get the directory path for both Android and IOS to store Database
    String databasesPath = await getDatabasesPath();
    String path = p.join(databasesPath, 'wifi.db');
    //Open/create the database at the given path
    var wifiSystemDatabase = await openDatabase(path, version: 1, onCreate: _createDb);
    return wifiSystemDatabase;
  }

希望对您有所帮助:)