Skip to content

Instantly share code, notes, and snippets.

Last active November 13, 2023 15:49
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import 'package:firedart/firedart.dart';
import 'package:hive/hive.dart';
/// Stores tokens using a Hive store.
/// Depends on the Hive plugin:
class HiveStore extends TokenStore {
static const keyToken = "auth_token";
static Future<HiveStore> create() async {
// Make sure you call both:
// Hive.init(storePath);
// Hive.registerAdapter(TokenAdapter(), adapterId);
var box = await Hive.openBox("auth_store",
compactionStrategy: (entries, deletedEntries) => deletedEntries > 50);
return HiveStore._internal(box);
final Box _box;
Token read() => _box.get(keyToken);
void write(Token token) => _box.put(keyToken, token);
void delete() => _box.delete(keyToken);
class TokenAdapter extends TypeAdapter<Token> {
final typeId = 42;
void write(BinaryWriter writer, Token token) =>
Token read(BinaryReader reader) =>
Token.fromMap(reader.readMap().map<String, dynamic>(
(key, value) => MapEntry<String, dynamic>(key, value)));
Copy link

guyluz11 commented Jun 2, 2021


  sdk: '>=2.12.0 <3.0.0'

This code will not work as it does not support the null safe (and it extends TokenStore).
You will get some errors.

In order to fix this you need to add ? to both @override Token read() and @override void write(Token token) override methods like this:

  Token? read() => _box.get(keyToken);

  void write(Token? token) => _box.put(keyToken, token);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment