我添加了备份和恢复功能的应用程序。但它只能在调试模式下正常工作,它不工作的最终版本和真实的的移动的。
有时备份和恢复工作在真实的的手机上,但当你删除应用程序并重新安装它,它不会恢复以前的数据。
/////////////////////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,
)),
),
我已向应用程序授予以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE "/>
<application
android:requestLegacyExternalStorage="true"
解决方法是什么?请指导我
1条答案
按热度按时间sqxo8psd1#
备份和恢复无法按照你想要的方式工作。你需要将要备份的内容发送到某个在线数据库(或云),要恢复,你也要从那里恢复。一旦你卸载了某个应用程序,与该应用程序相关的所有内容都会被清除。所以你无法实现你想要的。这不是Flutter特有的问题。这只是操作系统的工作方式。使用云解决方案进行备份和恢复。