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?
-
1Since 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– nvoigt2021-03-01 14:43:02 +00:00Commented 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.Dev94– Dev942021-03-01 14:47:35 +00:00Commented Mar 1, 2021 at 14:47
-
Just delete the file?nvoigt– nvoigt2021-03-01 15:08:07 +00:00Commented Mar 1, 2021 at 15:08
-
Did try flutter clean, but not workingDev94– Dev942021-03-01 15:08:51 +00:00Commented Mar 1, 2021 at 15:08
-
2Desktop 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– smorgan2021-03-02 00:20:47 +00:00Commented Mar 2, 2021 at 0:20
6 Answers
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
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
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
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
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,
);