我在 Flutter 中使用 Hive 数据库时得到了这个。在下面找出答案
因此,我创建了一个更简单的抽象级别,以便在我的 Flutter 应用程序中使用 Hive。这应该是管理和访问所有配置单元的中心点。由于eggetApplicationDocumentsDirectory在测试期间不可用,我如何才能测试整个文件?
import '../services/workout.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart' as path_rovider;
import 'workout.dart';
class HiveService {
static final HiveService _singleton = HiveService._internal();
static const String _workoutBox = "workoutBox";
factory HiveService() {
return _singleton;
}
HiveService._internal();
static Future<void> init() async {
final appDocumentDirectory =
await path_rovider.getApplicationDocumentsDirectory();
Hive.init(appDocumentDirectory.path);
Hive.registerAdapter(WorkoutAdapter());
}
static Future openWorkouts() {
return Hive.openBox<Workout>(_workoutBox);
}
static Future close() {
return Hive.close();
}
}
Run Code Online (Sandbox Code Playgroud) 我将列表保存到 Hive Box 中的索引中。
class Person {
String name;
Person(this.name);
}
List<Person> friends = [];
friends.add(Person('Jerry'));
var accountBox = Hive.openBox('account');
accountBox.put('friends',friends);
//Testing as soon as saved to make sure it's storing correctly.
List<Person> friends = accountBox.get('friends');
assert(friends.length == 1);
Run Code Online (Sandbox Code Playgroud)
所以这一切都按预期进行。由于某些疯狂的原因,当我热重启应用程序并尝试从 Hive 获取好友列表时,它不再返回List<Person>. 它返回一个List<dynamic>
var accountBox = Hive.openBox('account');
List<Person> friends = accountBox.get('friends');
///ERROR
E/flutter (31497): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled
Exception: type 'List<dynamic>' is not a subtype of type 'List<Person>'
E/flutter (31497): <asynchronous suspension>
etc...
Run Code Online (Sandbox Code Playgroud)
可能是什么原因造成的?这太不寻常了。
使用 Hive 缓存 API 结果的正确方法是什么?
目前我计划实现的方式是使用请求 URL 作为 key,使用返回的数据作为 body。
有没有适当的方法可以使生产更加友好?我找不到教程,因为大多数教程都是通过使用另一个包来抽象的,该包可以为他们处理这个问题,或者教程使用不同的包。
我试图在配置单元数据库中存储 Map<String, int>() 。我可以存储但无法检索它提示错误的 Map()
Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, int>?' in type cast
Run Code Online (Sandbox Code Playgroud)
代码:
数据控制者
Box<Map<String, int>> hiveBox = Hive.box('SaveBetBox');
void betStorage({String? matchKey, String? statKey, int? statIndex}) {
// Checks if the hive box has the data with following key if not adds
hiveBox.containsKey(matchKey)
? null
: hiveBox.put(
matchKey,
Map<String, int>(),
);
// Extract hive map to update or add Key values to Map with specific Key
Map<String, int>? tm = hiveBox.get(matchKey); …Run Code Online (Sandbox Code Playgroud) 我需要将颜色存储在我的 Hive 数据库中以便在我的电子商务应用程序中检索,它给了我下面的错误,说我需要制作一个适配器,谁能告诉我如何制作颜色适配器?
part 'items_model.g.dart';
@HiveType(typeId: 0)
class Item {
@HiveField(0)
final String name;
@HiveField(1)
final double price;
@HiveField(2)
final String? description;
@HiveField(3)
var image;
@HiveField(4)
final String id;
@HiveField(5)
final String shopName;
@HiveField(6)
final List<Category> category;
@HiveField(7)
Color? color;
@HiveField(8)
int? quantity;
Item({
required this.category,
required this.image,
required this.name,
required this.price,
this.description,
required this.id,
required this.shopName,
this.color,
required this.quantity,
});
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何生成或创建颜色适配器?因为我不知道怎么做
E/flutter ( 4621): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: HiveError: Cannot write, unknown type: MaterialColor. Did you forget to register …Run Code Online (Sandbox Code Playgroud) 我试图在我的应用程序中使用 Hive 作为 Flutter 中共享首选项的替代方案。但是,我不断收到一条错误消息:
I/flutter ( 4004): The method 'get' was called on null.
I/flutter ( 4004): Receiver: null
I/flutter ( 4004): Tried calling: get("counter", defaultValue: 0)
E/flutter ( 4004): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)]
Unhandled Exception: HiveError: This should not happen. Please open an
issue on GitHub.
Run Code Online (Sandbox Code Playgroud)
我遵循了pub.dev文档中显示的所有步骤,但是,我没有使用任何步骤TypeAdapters,我只是尝试使用int. 这是我的实现:
var box = Hive.box('box');
int counter;
void initHive() async {
await openBox();
getCounter(); //Updated code
}
Future openBox() async {
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
box …Run Code Online (Sandbox Code Playgroud) 我的 flutter 应用程序使用 Hive 进行本地数据管理,使用 Firebase 进行云数据存储。
我有一个使用 Firebase 包中的 Timestamp 和 FieldValue 的模式。我还创建了 TypeAdapter 并在主类中为我的自定义模式注册了它。
执行应用程序 Hive 时抛出以下错误:
未处理的异常:配置单元错误:无法写入,未知类型:时间戳。您是否忘记注册适配器?
问题:
如何为内置对象类型(例如Firebase 包中的Timestamp和FieldValue)创建 TypeAdapter (存在于类中loud_firestore.dart:)?
包裹:
dependencies:
hive: ^1.4.4
dev_dependencies:
hive_generator: ^0.7.1
build_runner:
Run Code Online (Sandbox Code Playgroud)
示例代码:
主要类别:
Hive.registerAdapter(SampleModalAdapter());
Run Code Online (Sandbox Code Playgroud)
模态代码:
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:hive/hive.dart';
part 'samplemodal.g.dart';
@HiveType(typeId: 1)
class SampleModal {
@HiveField(0)
String id;
@HiveField(1)
String title;
@HiveField(2)
Timestamp dateTime;
@HiveField(3)
FieldValue serverDateTime;
SampleModal({
this.id,
this.title,
this.dateTime,
this.serverDateTime,
});
}
Run Code Online (Sandbox Code Playgroud)
生成的 TypeAdapter 代码:
// GENERATED CODE - …Run Code Online (Sandbox Code Playgroud) 我有一个 Flutter 应用程序,我正在使用 Hive 来存储数据。
我删除了一些以前使用过的适配器。这导致了错误,我必须删除旧数据库。
现在,如果我推出更新,如何确保当用户更新应用程序时旧的 Hive 数据库被删除,这样就不会引起问题。
在我的 Flutter 应用程序中,我有一个包含 Player 对象的 Hive 框。只是为了调试,我在构造函数中输出来自玩家对象的一些数据,如下所示:
Player({
@required String name,
@required this.id,
}) {
this.name = name.toUpperCase();
print('${this.toString()}');
}
@override
String toString() {
return 'id: $id, name: $name';
}
Run Code Online (Sandbox Code Playgroud)
玩家像这样添加到框中,其中player.id是唯一的键:
playerBox.put(player.id, player);
Run Code Online (Sandbox Code Playgroud)
启动应用程序时,我还打印playerBox的值:
print (playerBox.values);
Run Code Online (Sandbox Code Playgroud)
这给了我所有添加的球员。到目前为止,一切都很好。但是...删除一个播放器后是这样的:
playerBox.delete(playerId);
Run Code Online (Sandbox Code Playgroud)
它开始表现得有点奇怪。当我重新启动应用程序时,已删除的播放器不再位于playerBox.values中,因此显然已从那里删除。但所有已删除的玩家对象的构造函数仍在运行。
检查堆栈,我可以看到这些对象确实是由 PlayerAdapter 实例化的。因此,由于某种原因,Hive 仍会从磁盘读取已删除的对象,但它们不在框中。
** 编辑
我还检查了模拟器上的player.hive文件,所有删除的模型实际上都在该文件中。即使它们被删除并且不会与playerBox.values一起返回
有什么想法可能是什么原因吗?这是预期的行为吗?