Floor ORM 中对象列表的类型转换器

发布于 2025-01-20 20:44:42 字数 281 浏览 0 评论 0原文

是否有任何方法可以将TypeConverter用于地板数据库中的对象列表,就像我们在Android的房间数据库中一样。

例如,我有具有对象字段列表的实体类;

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

}

如果地板不支持TypeConterter,您会建议使用哪种颤音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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2025-01-27 20:44:42

地板以最佳方式处理它,我的意思是您可以自由开发代码,以将所有类型的数据转换为地板支持的数据类型。

您可以使用这样的东西:

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);
  }
}

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文