:)
评级和组合对于 Hive 来说是未知类型。 我试图避免上课(尽管这很好,任何能让这项工作成功的事情......)。我只想保存进度,然后开始加载它。
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}');
}
有没有办法将它们转换为二进制内联? 我找到了这个 BinaryWriter 类:https://pub.dev/documentation/hive/latest/hive/BinaryWriter-class.html 但我不知道如何使用它,甚至不知道它是否适合我?
谢谢!
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 = {};
var reviewMapStored = {};
var front = [0];
var back = [1];
void main() async {
// if (Platform.isAndroid) SharedPreferencesAndroid.registerWith();
// if (Platform.isIOS) SharedPreferencesIOS.registerWith();
WidgetsFlutterBinding.ensureInitialized();
// Directory directory = await pathProvider.getApplicationDocumentsDirectory();
// Hive.init(directory.path);
await Hive.initFlutter();
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.toString(),
'ts': review.ts,
'rating': review.rating.toString()
};
print(reviewMap);
await box.add(reviewMap);
print(box.getAt(0));
reviewMapStored = box.getAt(0);
reviewMap['master'] = reviewMapStored['master'];
// reviewMap['combination'] = reviewMapStored['combination'];
reviewMap['ts'] = reviewMapStored['ts'];
// reviewMap['rating'] = reviewMapStored['rating'];
print(reviewMap['master']);
print(card.combination?.front);
if (reviewMapStored['combination'] == 'Combination([1]-[0])') {
reviewMap['combination'] = Combination(front: <int>[1], back: <int>[0]);
} else {
reviewMap['combination'] = Combination(front: <int>[0], back: <int>[1]);
}
if (reviewMapStored['rating'] == 'Rating.Hard' ) {
reviewMap['rating'] = Rating.Hard;
} else if (reviewMapStored['rating'] == 'Rating.Good' ) {
reviewMap['rating'] = Rating.Good;
} else if (reviewMapStored['rating'] == 'Rating.Easy' ) {
reviewMap['rating'] = Rating.Easy;
} else {
reviewMap['rating'] = Rating.Again;
}
print(reviewMapStored);
print(reviewMap);
review = Review(
master: reviewMap['master'],
combination: Combination(front: <int>[0], back: <int>[1]),
ts: reviewMap['ts'],
rating: Rating.Hard);
dolphin.addReviews(<Review>[review]);
print(reviewMap);
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);
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);
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);
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);
card = dolphin.nextCard()!;
printCard(card);
review = Review(
master: card.master,
combination: card.combination,
ts: DateTime.now(),
rating: Rating.Easy);
dolphin.addReviews(<Review>[review]);
stats =
dolphin.summary(); // => { due: 0, later: 0, learning: 10, overdue: 0 }
printStats(stats);
card = dolphin.nextCard()!;
printCard(card);
review = Review(
master: card.master,
combination: card.combination,
ts: DateTime.now(),
rating: Rating.Easy);
dolphin.addReviews(<Review>[review]);
stats =
dolphin.summary(); // => { due: 0, later: 0, learning: 10, overdue: 0 }
printStats(stats);
card = dolphin.nextCard()!;
printCard(card);
review = Review(
master: card.master,
combination: card.combination,
ts: DateTime.now(),
rating: Rating.Easy);
dolphin.addReviews(<Review>[review]);
stats =
dolphin.summary(); // => { due: 0, later: 0, learning: 10, overdue: 0 }
printStats(stats);
card = dolphin.nextCard()!;
printCard(card);
review = Review(
master: card.master,
combination: card.combination,
ts: DateTime.now(),
rating: Rating.Easy);
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}');
}