I need to store a list of strings inside drift database , i have actually set up a typeconverter to do that but i'm not sure exactly how to pass , i would appreciate anyone help , Thank you in advance.
- This is database table ( class called movies.drift )
CREATE TABLE movie (
id INT NOT NULL PRIMARY KEY AUTOINCREMENT,
title TEXT,
language TEXT,
voteAverage TEXT,
overView TEXT,
releaseDate TEXT,
movieImageUrl TEXT,
);
- This is my drift helper class
@DriftDatabase(include : {'movies.drift'})
class MovieDatabase extends _$MovieDatabase {
MovieDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
Future<List<MovieData>> getMovies() async {
return await select(movie).get();
}
Future<int> saveMovie(MovieCompanion companion) async {
return await into(movie).insert(companion);
}
// Future<int> deleteMovie(int id) async {
// return (delete(movie)..where(movie.id.equals(id))).go();
// }
Future<int> deleteAllMovies() async {
return await delete(movie).go();
}
Future<int> updateMovie(MovieCompanion companion) async {
return await update(movie).write(MovieCompanion(
title: Value(companion.title.toString())
));
}
}
LazyDatabase _openConnection(){
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'movies.db'));
return NativeDatabase(file);
});
}
- This is my type converter
class MovieTypeConverter extends TypeConverter<List<String>,String> {
const MovieTypeConverter();
@override
List<String>? mapToDart(String? fromDb) {
if (fromDb == null) return null;
final data = json.decode(fromDb);
return data;
}
@override
String? mapToSql(List<String>? value) {
if (value == null) return null;
return json.encode(value);
}
}
PS : I want to basically add List to my table and use typeconverter to save it into database and then retreive it .
Take a look at the docs on how to use them: https://drift.simonbinder.eu/docs/advanced-features/type_converters/
Example:
EDIT:
Your
TypeConvertershould have afromSqlandtoSqlmethod.Your drift file: