As below, I define ListConverter<T>
to make inheritance hierarchy.
abstract class ListConverter<T> extends TypeConverter<List<T>, String> {
static const delimiter = "!DELIMITER!";
T fromDB(String databaseValue);
String toDB(T value);
@override
List<T> decode(String databaseValue) {
return databaseValue
.split(delimiter)
.where((element) => element.isNotEmpty)
.map((e) => fromDB(e))
.toList();
}
@override
String encode(List<T> value) {
return value.map((e) => toDB(e)).join(delimiter);
}
}
Using ListConverter<T>
, I implement IntListConverter
.
class IntListConverter extends ListConverter<int> {
@override
int fromDB(String databaseValue) {
return int.parse(databaseValue);
}
@override
String toDB(int value) {
return value.toString();
}
}
My plan was to use ListConverter<T>
also on StringListConverter
, etc.
But unfortunately, I got this error message while running floor_generator
.
[SEVERE] floor_generator:floor_generator on lib/persistence/db/app_database.dart: Only classes that inherit from TypeConverter can be used as type converters. Make sure use a class that inherits from TypeConverter.
I know that Hierarchy (B) works well, but I want to build (A) structure for managing other list converters.
- (A)
TypeConverter -> ListConverter -> IntListConverter
- (B)
TypeConverter -> IntListConverter
How to make inheritance hierarchy for Type Converter of Dart Floor?
CodePudding user response:
I resolved this problem in a roundabout way.
I use mixin
keyword instead of direct inheritance.
class IntListConverter extends TypeConverter<List<int>, String>
with ListConverter<int> {
@override
int fromDB(String databaseValue) {
return int.parse(databaseValue);
}
@override
String toDB(int value) {
return value.toString();
}
}