Hive: Does hive support adapters for external source files ?

Created on 11 Jul 2020  Â·  5Comments  Â·  Source: hivedb/hive

Question
Please explain the problem you are running into.
I need an adapter for GeoPoint and I dont see any way to create an adapter for an external source file in Hive docs. is this even supported ?

Code sample

Provide a few simple lines of code to show your problem.

Version

  • Platform: iOS, Android, Mac, Windows, Linux, Web
  • Flutter version: [e.g. 1.5.4]
  • Hive version: [e.g. 0.5.0]
needs-author-feedback question

Most helpful comment

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

All 5 comments

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

You can check create adapter manually section from hive docs.

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

Thank you @marcelgarus What about DocumentSnapshot ? I've been at it for days now and I havent have any luck. I read the documentation for creating an adapter manually but I cant figure out how to do it for DocumentSnapshot

I just took a look at DocumentSnapshot and there's no way to serialize that using Hive – or any other serializer for that matter. The reason is that it's not a pure data-like class, but a complete interactive object – for example, it contains a reference to the current FirebaseFirestore instance, which you simply can't save to Hive because it represents an active Firestore connection. Also, the constructor is private, so it can only be called by Firestore itself.

You should rather save the actual data itself to Hive instead of the snapshot containing the data. For example, if you have a DocumentSnapshot<User>, you should save the User to Hive.

That's what I thought so implemented a solution in my code of not saving the DocumentSnapshot data to Hive.

Thank you @marcelgarus

Was this page helpful?
0 / 5 - 0 ratings