I am new with flutter, so let me know if I am missing anything. I have the folowing situation:
I am trying to create the table entity
. The problem happens when I try to retrieve the data, I get an error saying that the table does not exist. This error does not happen when I insert into the same table. It is obvious that the table is not being created, so I would like to know what I am doing wrong on the creation.
Additionally, as the error is only being shown when I retrieve and not when I insert, I am curious about that. So, can someone explain me about this, like why it happens?
Below I am leaving the code that I think is necessary, please let me know if something else is needed.
Future<Database> createDatabase() {
return getDatabasesPath().then((dbPath) {
final String path = join(dbPath, "generic-app.db");
return openDatabase(path, onCreate: (db, version) {
db.execute("CREATE TABLE entity ("
"id INTEGER PRIMARY KEY"
"titulo VARCHAR,"
"subtitulo VARCHAR"
")");
}, version: 1);
});
}
The save() method that insert into the table:
Future<int> save(Entity entity) {
return createDatabase().then((db) {
final Map<String, dynamic> mapEntity = Map();
mapEntity['id'] = entity.id;
mapEntity['titulo'] = entity.titulo;
mapEntity['subtitulo'] = entity.subtitulo;
return db.insert("entity", mapEntity);
});
}
The findAll() method that is throwing the exception:
Future<List<Entity>> findAll() {
return createDatabase().then((db) {
return db.query("entity").then((maps) {
List<Entity> entities = List.empty(growable: true);
for (Map<String, dynamic> map in maps) {
Entity entity = Entity(
map['id'],
map['titulo'],
map['subtitulo'],
);
entities.add(entity);
}
return entities;
});
});
}
The stacktrace shown:
E/SQLiteLog(21931): (1) no such table: entidade in "SELECT * FROM entity"
E/flutter (21931): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception:
DatabaseException(no such table: entity (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM
entity) sql 'SELECT * FROM entity' args []
E/flutter (21931): #0 wrapDatabaseException (package:sqflite/src/exception_impl.dart:11:7)
E/flutter (21931): <asynchronous suspension>
E/flutter (21931): #1 SqfliteDatabaseMixin.txnRawQuery.<anonymous closure>
(package:sqflite_common/src/database_mixin.dart:404:30)
E/flutter (21931): <asynchronous suspension>
E/flutter (21931): #2 BasicLock.synchronized
(package:synchronized/src/basic_lock.dart:33:16)
E/flutter (21931): <asynchronous suspension>
E/flutter (21931): #3 SqfliteDatabaseMixin.txnSynchronized
(package:sqflite_common/src/database_mixin.dart:344:14)
E/flutter (21931): <asynchronous suspension>
Delete the app from the emulator or the physical device and re-install it for the db to be initialized and all the neccesasry tables will be created.