I added backup and restore feature to the app. But it only works properly in debug mode and it doesn't work in the final version and on the real mobile.
Sometimes backup and restore works on the real phone, but when you delete the application and reinstall it, it does not restore the previous data.
/////////////////////Backup Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
final String? pathMyCart =
Hive.box<Cart>(cartBoxName).path;
final String? pathPersonCart =
Hive.box<CartPerson>(cartPersonBoxName).path;
String myCartName = 'carts.hive';
String personCartName = 'person.hive';
String path = await ExternalPath
.getExternalStoragePublicDirectory(
ExternalPath.DIRECTORY_DOWNLOADS);
String mainPath = '$path/MyCarts/Backup/';
await Permission.storage.request();
await Directory(mainPath).create(recursive: true);
Directory dirMyCart = Directory('$mainPath$myCartName');
Directory dirPersonCart =
Directory('$mainPath$personCartName');
String mainDirMyCart = dirMyCart.path;
String mainDirPersonCart = dirPersonCart.path;
File fileMyCartBackup = File('$pathMyCart');
File filePersonCartBackup = File('$pathPersonCart');
await fileMyCartBackup.copy(mainDirMyCart);
await filePersonCartBackup.copy(mainDirPersonCart);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Backup completed successfully',
style: themeData.textTheme.headline6!
.copyWith(color: themeData.primaryColor),
),
backgroundColor:
themeData.textTheme.bodyText1!.color,
),
);
},
child: Text(
'Backup',
style: themeData.textTheme.headline4!
.copyWith(color: themeData.backgroundColor),
),
),
),
/////////////////////Restore Button
SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
var status = await Permission.storage.status;
if (!status.isRestricted) {
await Permission.storage.request();
}
final String? pathMyCart =
Hive.box<Cart>(cartBoxName).path;
final String? pathPersonCart =
Hive.box<CartPerson>(cartPersonBoxName).path;
String myCartName = 'carts.hive';
String personCartName = 'person.hive';
String path = await ExternalPath
.getExternalStoragePublicDirectory(
ExternalPath.DIRECTORY_DOWNLOADS);
String mainPath = '$path/MyCarts/Backup/';
Directory dirMyCart =
Directory('$mainPath$myCartName');
Directory dirPersonCart =
Directory('$mainPath$personCartName');
String mainDirMyCart = dirMyCart.path;
String mainDirPersonCart = dirPersonCart.path;
File fileMyCartBackup = File(mainDirMyCart);
File filePersonCartBackup = File(mainDirPersonCart);
fileMyCartBackup.copy('$pathMyCart');
filePersonCartBackup.copy('$pathPersonCart');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'The recovery was successful',
style: themeData.textTheme.headline6!
.copyWith(color: themeData.primaryColor),
),
backgroundColor:
themeData.textTheme.bodyText1!.color,
),
);
},
child: Text(
'recovery',
style: themeData.textTheme.headline4,
)),
),
I have given the following permissions to the application
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE "/>
<application
android:requestLegacyExternalStorage="true"
what's the solution? please guide me
Backup and restore can't work the way you want. You need to be sending what is to be backed up to some database online (or to the cloud) and to restore, you restore from there too. Everything related to an app is cleared once you uninstall that app. So you can't achieve what you want. It's not a Flutter specific issue. It's just how Operating Systems work. Use a cloud solution for backup and restore.