Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typeconverter for list of objects in Floor ORM

Is there any way to use typeconverter for list of objects in floor database, like we have in room database for android.

For example i have entity class which has field list of objects;

@Entity(tableName: "example")
class Example {
  @PrimaryKey()
  String id;
  ...
  List<AnotherObject> objects;

}

If floor doesn't support typeconverters which flutter ORM would you recommend to use?

like image 649
Konstantine Tirkia Avatar asked Oct 25 '25 02:10

Konstantine Tirkia


1 Answers

floor handles it in the best way, I mean you are free to develop your code for converting all types of data to a data type that is supported by floor.

You can use something like this:

class ListSubCategoryFinanceConverter
    extends TypeConverter<List<SubCategoryFinance>, String> {
  @override
  List<SubCategoryFinance> decode(String databaseValue) {
    final jsonFile = json.decode(databaseValue);
    List<SubCategoryFinance> finances = [];
    finances = List.from(jsonFile['subCats'])
        .map((e) => SubCategoryFinance.fromMap(jsonDecode(e)))
        .toList();

    return finances;
  }

  @override
  String encode(List<SubCategoryFinance> value) {
    final data = <String, dynamic>{};
    data.addAll({'subCats': value});
    return json.encode(data);
  }
}
like image 139
Reza Taghizadeh Avatar answered Oct 26 '25 17:10

Reza Taghizadeh