8

i'm working on flutter desktop app and i want to clear app database, Like as we do on mobile, we go to app settings and clear cache and data, to reset app settings. Likewise i want to do the same with desktop app. Can anyone help me how to do that?

Thanks

Dev94
  • 757
  • 6
  • 24
  • 1
    Since this is a feature of your operating system and most desktop operating systems don't have that feature, I doubt there will be an easy explanation. How are you saving the data you want cleared? – nvoigt Mar 01 '21 at 14:43
  • I'm using sqflite_common_ffi plugin for local database in desktop app. I want to clear that database and rebuild it. – Dev94 Mar 01 '21 at 14:47
  • Just delete the file? – nvoigt Mar 01 '21 at 15:08
  • Did try flutter clean, but not working – Dev94 Mar 01 '21 at 15:08
  • No, the database file. How do you open your database? – nvoigt Mar 01 '21 at 15:09
  • How can i access database file for flutter desktop app? – Dev94 Mar 01 '21 at 16:43
  • Well, how do you access other files? Using your file explorer. Can you post the code on how you open your database? – nvoigt Mar 01 '21 at 16:44
  • 2
    Desktop is three different OSes, each of which stores user data in a different place. You should clarify which platform you are taking about because the answers will be different. – smorgan Mar 02 '21 at 00:20
  • Any success? How to clear data in Windows? – AVEbrahimi Apr 18 '21 at 06:36
  • At the time creation of database for the first time we allocate it some path, I found path from there and then manually delete the file from that path. Flutter clean only clear the build files not database file. – Dev94 Jul 08 '21 at 17:34
  • Does anyone found path to the cache for Windows desktop app build? – yuralife Dec 17 '21 at 12:41

6 Answers6

3

I was able to fix this in Linux by deleting the flutter_app_directory found in ~/.local/share/<flutter_app_name>

Similar actions can be found in Windows and MacOS

Bhikkhu Subhuti
  • 434
  • 2
  • 12
2
 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);
    },
  );

Just print the path of your database and you will know exactly where it is stored, for me, its store on this path

flutter: C:\Users\USERNAME\AppData\Roaming\PACKAGE_NAME\debt_record\db.sqlite

Mahmood Ali
  • 487
  • 1
  • 6
  • 19
  • I found my shared preferences and database in the path C:\Users\khadi\AppData\Roaming\{you application} and able to clear app preferences and databases if any. – Dev94 Sep 13 '22 at 11:09
2

My issue was related to Hive on Windows, I managed to solve it with the following solution (i think it works on other platforms as well) :

 await Hive.initFlutter();

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

also don't forget to import the path provider as follow:

import 'package:path_provider/path_provider.dart';

you will get something like this on the debug console

C:\Users\USERNAME\Documents

when you navigate to that directory you will find the files you need, for hive, I found all the boxes files like box_name.hive and box_name.lock so I deleted the files related to my issue and that resolved it.

this would work with all the other storage packages as well

Fethi
  • 766
  • 7
  • 18
0

for macos i had to open finder then hit command + shift + g and paste this

~/Library/Application Support

and in app package folder I found all the files

0

or you call clear() as seen in the 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,
  );
Noob
  • 710
  • 11
  • 15
0

I had the same need today, on MacOS Ventura. I had conflicts on a SQLite database schema and even with flutter clean it didn't disapeared. Only was was to delete the previous .sqlite file (of course, this only work in early stage of your app development, stay retro-compatible with your previous database schemas as soon as you have real users with device installs).

Find the path to database

sudo find / -name {my_database_name}.sqlite

This gave me the following result:

/System/Volumes/Data/Users/{my_username}/Library/Containers/{full_qualified_app_name}/Data/Documents/{my_database_name}.sqlite

Example:

/System/Volumes/Data/Users/gerfaut/Library/Containers/com.example.todoapp/Data/Documents/tasks.sqlite

Delete the sqlite file

  • with Finder: Hit command + shift + g then paste the path to the file and manually delete it
  • or with Terminal: rm /System/Volumes/Data/Users/{my_username}/Library/Containers/{full_qualified_app_name}/Data/Documents/{my_database_name}.sqlite

Relaunch your Flutter app and a new database is created!

Gerfaut
  • 119
  • 1
  • 3
  • 13