There are various articles on how to import data on the internal database from a csv, from another database etc .. but I haven't found anyone explaining how to export the flutter database.
The goal is to create a backup for each cell phone. (So I need to understand where it is located for create a backup)
I assume you are using sqflite
plugin for SQLite operations and path_provider
for storage. The path of the database can be found using
String path = await getDatabasesPath(); // which is data/data/<package_name>/databases
Additionally, sqflite
plugin doesn't provide any way to import/export database, here is an open issue, if you really want to do it, you will have to do it natively using MethodChannel
, here is the solution for Android and AFAIK there is no way to do it in iOS.
if you use Sqlite to create database:
Step 1: When you create database, you set a directory for it, you can use path_provider like this:
var dir = await getApplicationDocumentsDirectory();
_dbPath = dir.path + '/$dbName';
so now you know what directory and path it is.
Step 2: Then use flutter_archive
plugin to zip the file (This will zip the file in a directory, which is your db);
Step 3: use flutter_email_sender to send it by email, like this:
final email = Email(
body: 'content',
subject: 'content',
recipients: ['email'],
cc: ['email'],
attachmentPaths: [exportPath],
isHTML: false,
);
await FlutterEmailSender.send(email);
Your need to provide exportPath
, which is the zip file path you set up.
It worked for us, hope this will help other people!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With