We have an app build entirely by flutter. When we send app upgrades to google store the sqflite database data is not lost when updating apk in Android device. But in iOS Device after user updates app to new version all database data lost.
flutter v1.17.5
Package used for data persistence:
sqflite: ^1.3.1 path_provider: ^1.6.11
Can you please help to solve this problem. Data of user must not be deleted after app update to new version. Maybe I need to use other type of data persistence or how can I solve this.
Here are parts where I initialize Database in 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)');
}
Thanks very much
I think your DB path is getting changed. Try this.
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;
}
Hope it helps :)