13

I'm working on a flutter desktop app, and I want to clear the app database. Like as we do on mobile, we go to app settings and clear cache and data, to reset the app settings. Likewise, how can I do the same with a desktop app?

11
  • 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? Commented Mar 1, 2021 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. Commented Mar 1, 2021 at 14:47
  • Just delete the file? Commented Mar 1, 2021 at 15:08
  • Did try flutter clean, but not working Commented Mar 1, 2021 at 15:08
  • 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. Commented Mar 2, 2021 at 0:20

6 Answers 6

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

need a path for MAcOS
or ~/Library/Containers/<flutter_app_name> for macos
4

My issue was related to Hive on Windows, and 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.

Comments

3

Use:

 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, it's stored in this path:

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

1 Comment

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.
0

For macOS, I had to open Finder and then hit Command + Shift + G and paste this:

~/Library/Application Support

And in the app package folder, I found all the files.

Comments

0

I had the same need today, on macOS v13 (Ventura). I had conflicts on a SQLite database schema and even with Flutter clean it didn't disappeared. Only was was to delete the previous .sqlite file (of course, this only works 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 the 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, 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!

Comments

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,
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.