Reputation: 877
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?
Upvotes: 12
Views: 5797
Reputation: 1
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,
);
Upvotes: 0
Reputation: 516
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
Upvotes: 3
Reputation: 542
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.
Upvotes: 5
Reputation: 119
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).
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
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!
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 927
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.
Upvotes: 4