Flutter-重新启动应用程序时清除应用程序数据



我想在应用程序启动或关闭时清除所有应用程序数据和缓存。我的目的是在每次打开应用程序时第一次启动它。在我的main.dart文件中,我使用以下代码和插件path_provider:

Future<void> _deleteCacheDir() async {
final cacheDir = await getTemporaryDirectory();
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);
}
}
Future<void> _deleteAppDir() async {
final appDir = await getApplicationSupportDirectory();
if (appDir.existsSync()) {
appDir.deleteSync(recursive: true);
}
}
void main() async {
_deleteCacheDir(); // To clear cache
_deleteAppDir(); // To clear app's storage
WidgetsFlutterBinding.ensureInitialized(); // Initialize plugins
await Firebase.initializeApp(); // Initialize Firebase
var initializationSettingsAndroid =
AndroidInitializationSettings('circle_ic_appliguide');
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification:
(int? id, String? title, String? body, String? payload) async {});
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
});
runApp(MyApp()); // Run the App
}

但它似乎不起作用。。。我是不是用了好方法来释放我的目的?

获取临时文件夹并删除其中的文件

var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);

相关内容

  • 没有找到相关文章

最新更新