如何像flutter移动的应用一样清除flutter桌面应用数据

zujrkrfu  于 2022-11-17  发布在  Flutter
关注(0)|答案(5)|浏览(222)

我正在处理flutter桌面应用程序,我想清除应用程序数据库,就像我们在移动的上所做的那样,我们转到应用程序设置并清除缓存和数据,以重置应用程序设置。同样,我想对桌面应用程序做同样的事情。有人能帮助我如何做到这一点吗?
谢谢

s1ag04yj

s1ag04yj1#

在Linux中,我通过删除~/.local/share/中的flutter_app_目录修复了这个问题。<flutter_app_name>
在Windows和MacOS中可以找到类似的操作

ie3xauqp

ie3xauqp2#

return LazyDatabase(
    () async {
      var dbFolderPath = '';
      if (GetPlatform.isDesktop) {
        final provider = PathProviderWindows();
        dbFolderPath = (await provider.getApplicationSupportPath())!;
      } else {
        final dbFolder = await getApplicationDocumentsDirectory();
        dbFolderPath = dbFolder.path;
      }

      final file = File(path.join(dbFolderPath, _databaseName));
      debugPrint(file.path);
      return NativeDatabase(file);
    },
  );

只需打印数据库的路径,您就可以知道它存储的确切位置,对我来说,它存储在此路径上
flutter:C:\用户\用户名\应用程序数据\漫游\包名\债务记录\db.sqlite

js5cn81o

js5cn81o3#

对于macos,我不得不打开finder,然后点击command + shift + g并粘贴这个
~/Library/Application Support
在app package文件夹中我找到了所有的文件

wlzqhblo

wlzqhblo4#

我的问题是在Windows上的Hive,我设法用下面的解决方案解决了它(我认为它在其他平台上也能工作):

await Hive.initFlutter();

 var appDir = await getApplicationDocumentsDirectory();
 print(appDir);

也不要忘记导入路径提供程序,如下所示:

import 'package:path_provider/path_provider.dart';

您将在调试控制台上看到如下内容

C:\Users\USERNAME\Documents

当您导航到该目录时,您将找到所需的文件,对于配置单元,我找到了所有框文件,如box_name.hivebox_name.lock,因此我删除了与我的问题解决相关的文件
这也适用于所有其他存储包

yyhrrdl8

yyhrrdl85#

或者调用clear(),如readme中所示

// Create a box collection
  final collection = await BoxCollection.open(
    'MyFirstFluffyBox', // Name of your database
    {'cats', 'dogs'}, // Names of your boxes
    path: './', // Path where to store your boxes (Only used in Flutter / Dart IO)
    key: HiveCipher(), // Key to encrypt your boxes (Only used in Flutter / Dart IO)
  );

  // Open your boxes. Optional: Give it a type.
  final catsBox = collection.openBox<Map>('cats');

  // Put something in
  await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
  await catsBox.put('loki', {'name': 'Loki', 'age': 2});

  // Get values of type (immutable) Map?
  final loki = await catsBox.get('loki');
  print('Loki is ${loki?['age']} years old.');

  // Returns a List of values
  final cats = await catsBox.getAll(['loki', 'fluffy']);
  print(cats);

  // Returns a List<String> of all keys
  final allCatKeys = await catsBox.getAllKeys();
  print(allCatKeys);

  // Returns a Map<String, Map> with all keys and entries
  final catMap = await catsBox.getAllValues();
  print(catMap);

  // delete one or more entries
  await catsBox.delete('loki');
  await catsBox.deleteAll(['loki', 'fluffy']);

  // ...or clear the whole box at once
  await catsBox.clear(); //<----------------------------------------------

  // Speed up write actions with transactions
  await collection.transaction(
    () async {
      await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4});
      await catsBox.put('loki', {'name': 'Loki', 'age': 2});
      // ...
    },
    boxNames: ['cats'], // By default all boxes become blocked.
    readOnly: false,
  );

相关问题