hive icon indicating copy to clipboard operation
hive copied to clipboard

When using a Map of Set, code generation for Adapters doesn't compile

Open fodil-a opened this issue 4 years ago • 2 comments
trafficstars

Steps to Reproduce Use HiveType on class with field of type Map<int, Set>

Code sample

@HiveType(typeId: 0)
class Foo {
  @HiveField(0)
  Map<int, Set<int>> map;
}

Generates code that does not compile (because v is not a List) :

class FooAdapter extends TypeAdapter<Foo> {
  @override
  final int typeId = 0;

  @override
  Foo read(BinaryReader reader) {
    final numOfFields = reader.readByte();
    final fields = <int, dynamic>{
      for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
    };
    return Foo()
      ..map = (fields[0] as Map)?.map((dynamic k, dynamic v) =>
          MapEntry(k as int, (v as List)?.cast<int>()));
  }

  @override
  void write(BinaryWriter writer, Foo obj) {
    writer
      ..writeByte(1)
      ..writeByte(0)
      ..write(obj.map);
  }

  @override
  int get hashCode => typeId.hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is FooAdapter &&
          runtimeType == other.runtimeType &&
          typeId == other.typeId;
}

Version

  • Platform: iOS, Android, Mac, Windows, Linux, Web
  • Flutter version: 1.27.2.0.pre.96
  • Hive versions
    • hive: ^1.4.4+1
    • hive_flutter: ^0.3.1
    • hive_generator: ^0.8.2
    • build_runner: ^1.11.5

fodil-a avatar Feb 21 '21 18:02 fodil-a

Related to https://github.com/hivedb/hive/issues/410. Based on this comment, it would seem that Sets are only supported through custom adapters :(

bgetsug avatar Apr 08 '21 01:04 bgetsug

This should be fixed in hive_ce: https://pub.dev/packages/hive_ce

Rexios80 avatar Jul 05 '24 23:07 Rexios80