Trying to save “reviews” in hive. Is there a way to convert data to binary inline?

  Kiến thức lập trình

🙂

Rating and combination is an unknown type for hive.
I’m trying to avoid classes. I just want to save the progress, and at start loading it back.

import 'package:dolphinsr_dart/dolphinsr_dart.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path_provider/path_provider.dart' as pathProvider;
import 'dart:convert';

var reviewMap = {};

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Directory directory = await pathProvider.getApplicationDocumentsDirectory();
  Hive.init(directory.path);

  var box = await Hive.openBox('testBox');

  final reviews = <Review>[];
  final dolphin = DolphinSR();

  dolphin.addMasters(<Master>[
    Master(id: '1', fields: <String>[
      'คน',
      'person'
    ], combinations: <Combination>[
      Combination(front: <int>[0], back: <int>[1]),
      Combination(front: <int>[1], back: <int>[0]),
    ]),
    Master(id: '2', fields: <String>[
      'คบ',
      'To date'
    ], combinations: <Combination>[
      Combination(front: <int>[0], back: <int>[1]),
      Combination(front: <int>[1], back: <int>[0]),
    ])
  ]);
  dolphin.addReviews(reviews);

  var stats =
      dolphin.summary(); // => { due: 0, later: 0, learning: 2, overdue: 0 }

  printStats(stats);

  var card = dolphin.nextCard()!;
  printCard(card);
  var review = Review(
      master: card.master,
      combination: card.combination,
      ts: DateTime.now(),
      rating: Rating.Hard);
  dolphin.addReviews(<Review>[review]);

  reviewMap = {
    'master': review.master,
    'combination': review.combination,
    'ts': review.ts,
    'rating': review.rating
  };
  print(reviewMap);
  await box.add(reviewMap);
  print(box.getAt(0));

  card = dolphin.nextCard()!;
  printCard(card);

  review = Review(
      master: card.master,
      combination: card.combination,
      ts: DateTime.now(),
      rating: Rating.Hard);
  dolphin.addReviews(<Review>[review]);

  stats =
      dolphin.summary(); // => { due: 0, later: 0, learning: 10, overdue: 0 }
  printStats(stats);
}

void printCard(DRCard card) {
  print(
      '${card.master}-${card.back}-${card.front}-${card.combination!.back}-${card.combination!.front} - ${card.lastReviewed} - ${card.dueDate}');
}

void printStats(stats) {
  print('${stats.due}-${stats.later}-${stats.learning}-${stats.overdue}');
}

Is there a way to convert them to binary inline?
I have found this BinaryWriter class: https://pub.dev/documentation/hive/latest/hive/BinaryWriter-class.html
But I don’t know how to use it or even if it is suitable for me?

Thanks!

LEAVE A COMMENT