Baran
Baran

Reputation: 394

Hive: type 'x' is not a subtype of type 'y' of 'obj'

I have a ServerData class to send data to server, but if somehow the data can not be sent, I have to save the unsent data to local storage to send it again in another time.

@HiveType(typeId: 0)
class ServerData {
  @HiveField(0)
  final String roomId;
  @HiveField(1)
  final DateTime scanFinishedTime;
  @HiveField(2)
  final WifiAndBluetoothEvent wifiAndBluetoothEvent;
}

Than WifiAndBluetoothEvents:

@HiveType(typeId: 1)
class WifiAndBluetoothEvent {
  @HiveField(3)
  List<WifiEvent> wifiEvents;
  @HiveField(4)
  List<BluetoothEvent> bluetoothEvents;
}

Than BluetoothEvent:

@HiveType(typeId: 2)
class BluetoothEvent extends Equatable {
  @HiveField(5)
  final DateTime scanTime;
  @HiveField(6)
  final String name;
  @HiveField(7)
  final String macAddress;
  @HiveField(8)
  final BluetoothDeviceType? type;
  @HiveField(9)
  final int rssi;
}

And finally WifiEvent:

@HiveType(typeId: 3)
class WifiEvent extends Equatable {
  @HiveField(10)
  final String name;
  @HiveField(11)
  final String macAddress;
  @HiveField(12)
  final WiFiStandards standard;
  @HiveField(13)
  final int rssi;
  @HiveField(14)
  final WiFiChannelWidth? channelWidth;
  @HiveField(15)
  final int frequency;
  @HiveField(16)
  final DateTime timestamp;
  @HiveField(17)
  final DateTime receivedTime;
}

And I have registered them in main:

for (final adapter in adaptersToRegister) {
    Hive.registerAdapter(adapter);
  }

  final directory = await getApplicationDocumentsDirectory();
  Hive.init(
    directory.path,
  );

But I am getting type 'ServerData' is not a subtype of type 'BluetoothEvent' of 'obj' error when I try to write data to local storage.

Upvotes: 2

Views: 373

Answers (1)

Mohamed Gawdat
Mohamed Gawdat

Reputation: 121

as I had the same problem you can't path the classes as a type to initialize it in adapter so, you have to init each adapter without looping Example :

Example of using adpters

or you can use Generics Like This

Upvotes: 2

Related Questions