【问题标题】:Flutter Hive model have enum type but not able to put/set value in enum with hive-fieldFlutter Hive 模型具有枚举类型,但无法在具有蜂巢字段的枚举中放置/设置值
【发布时间】:2023-02-06 13:51:14
【问题描述】:

我想要一个 hivemodel 类,它包含已提交的枚举,但 hive 无法设置值。

联系方式是枚举:

@freezed
@HiveType(typeId: 7, adapterName: "FriendGroupEntityAdapter")
@freezed

class FriendsGroupEntity with _$FriendsGroupEntity {

  const factory FriendsGroupEntity({
 @HiveField(0)  int userId,
  @HiveField(1)  @Default(CONTACTTYPE.loop) CONTACTTYPE contactType,
  @HiveField(2)   String contact,
  @HiveField(3)   int id,
  @HiveField(4)   int value,
  @HiveField(5)   int value2,
  @HiveField(6)   int value3,
  @HiveField(7)   int value7,
});

这里 CONTACTTYPE.loop 是一个枚举,我想将它存储到配置单元中,并得到一个我附加的错误。

蜂巢中有枚举的方法吗?

【问题讨论】:

  • 你能把代码段放在问题中吗
  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。
  • 更新了问题,请查看
  • 伙计们,需要帮助

标签: flutter hive local-storage flutter-hive


【解决方案1】:

请按照这种方法

import 'package:hive/hive.dart';
    
    part 'myrepresentation.g.dart';
    
    @HiveType(typeId: 1)
    class FriendsGroupEntity extends HiveObject {
      @HiveType(0)
      final String id;
    
      @HiveType(1)
      final Foo foo;
      MyRepresentation({required this.id, required this.foo});
    }
    
    @HiveType(typeId: 2)
    enum Foo {
      @HiveField(0)
      foo,
      @HiveField(1)
      bar,
    }

检查此参考link

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多