Closed lopallo closed 1 year ago
Hard to know what is wrong without more information (stack trace ?, platform...).
It is important to have a type in your function i.e.:
Future importDb(String saved) async {
instead of:
Future importDb(saved) async {
Aer you sure it crashes during importDatabase? (and not just in jsonDecode...)
The program is written in flutter for android. The parameter "saved" comes from this code: Center( child: ElevatedButton( onPressed: () async { final result = await FilePicker.platform.pickFiles(); if (result == null) return; final file = result.files.single.path; db.importDb(file); }, child: const Text('Importa il database')), )
I think the problem is in jsonDecode
(Please use triple backticks to format your code as it is hard to read, see markdown help)
Add some temporary print to see what you are indeed trying to decode.
Future<void> importDb(String saved) async {
// tmp
print(saved);
As I understand, saved
is a json encoding of your exported database that you want to import
In your code, your are passing a file path not a json content:
final result = await FilePicker.platform.pickFiles();
if (result == null) return;
final file = result.files.single.path;
db.importDb(file);
You might want to read the content first, for example for io app:
final file = File(result.files.single.path);
var content = await file.readAsString();
await db.importDb(content);
Thank you, I will try with these tips
Hi, I went ahead and managed to read the database. The code is this: child: ElevatedButton( onPressed: () async { // final result = await FilePicker.platform.pickFiles(); // if (result == null) return;
final directory = await getExternalStorageDirectory();
final path = directory?.path;
File file = File('$path/titoli.json');
var dbTitoli = file.readAsStringSync();
await db.importDb(dbTitoli);
},
child: const Text('Importa il database')),
I can also see it temporarily but if I restart the app the data is lost. I think I miss saving data that I think only remains in memory, is that correct? How do I save it? This is the code:
Future importDb(dbTitoli) async {
final map = await jsonDecode(dbTitoli);
final directory = await getExternalStorageDirectory();
final path = directory?.path;
_dbTitolo = await importDatabase(map, dbTitoloFactory, '$path/titolo.db');
}
The database should be available where you imported it, i.e. at $path/titolo.db
, when you reopen the app.
I don't know on which platform you are running but make sure you have the proper rights to write the external storage directory.
Hi, my platform is flutter for android. After running flutter clean on visul studio code the problem has changed. Now no data matters. The code is as follows:
Future importDb(dbTitoli) async {
final map = await jsonDecode(dbTitoli);
final directory = await getExternalStorageDirectory();
final path = directory?.path;
print(map);
_dbTitolo = await importDatabase(map, dbTitoloFactory, '$path/titolo.db');
print(_dbTitolo);
}
print(map); Print this Data: I/flutter ( 5566): {sembast_export: 1, version: 1, stores: [{name: titoli, keys: [7, 8, 9], values: [{id: 0, titolo: test 1}, {id: 0, titolo: test 2}, {id: 0, titolo: test 3}]}]}
print(_dbTitolo); Print this Data: I/flutter ( 5566): {path: /storage/emulated/0/Android/data/com.lopallo.checklist_ambulanza/files/titolo.db, version: 1, stores: [{name: _main, count: 0}, {name: titoli, count: 2}], exportStat: {lineCount: 3, obsoleteLineCount: 0, compactCount: 0}}
I solved, I was wrong the directory getExternalStorageDirectory the correct code: final directory = await getApplicationDocumentsDirectory();
I am importing a previously exported database. My code:
Future importDb(saved) async { var map = jsonDecode(saved) as Map; var importDb = await importDatabase(map, dbTitoloFactory, 'titolo.db');
this is the database file: {"sembast_export":1,"version":1,"stores":[{"name":"titoli","keys":[3,4,5],"values":[{"id":0,"titolo":"pippo"},{"id":0,"titolo":"pluto"},{"id":0,"titolo":"paperino"}]}]}
the console vs code gives me this error: E/flutter (20571): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at character 1)