Flutter Hive首次加载初始值问题
我有这些错误,我不知道有什么问题, 这是一个新创建的蜂巢数据,尚未包含任何值。 我正在尝试加载它,如果值= null return defaultValue
这是蜂巢负载的代码:
hiveOperation() async {
GlobalPosition globalPosition;
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
await Hive.openBox('box');
Hive.registerAdapter(GlobalPositionAdapter());
globalPosition = box.get('globalPosition');
lat = globalPosition.latitude;
lon = globalPosition.longitude;
}
这是Hive构造器:
import 'package:hive/hive.dart';
part 'storage.g.dart';
@HiveType(typeId: 1)
class GlobalPosition {
GlobalPosition({required this.latitude, required this.longitude});
@HiveField(0, defaultValue: 1.0)
double latitude;
@HiveField(1, defaultValue: 1.0)
double longitude;
}
加载时的错误: [错误:flutter/lib/ui/ui_dart_state.cc(209)]毫无用处的例外:type'null'不是类型'globalposition'的子类型
,我不知道如何修复它,请帮助我,谢谢!
I have these errors and i don't know what's the problem,
it's a newly created hive data and doesn't contain any value yet.
I'm trying to load it and if value = null return defaultValue
here's the code for hive load :
hiveOperation() async {
GlobalPosition globalPosition;
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
await Hive.openBox('box');
Hive.registerAdapter(GlobalPositionAdapter());
globalPosition = box.get('globalPosition');
lat = globalPosition.latitude;
lon = globalPosition.longitude;
}
this is the hive constructor :
import 'package:hive/hive.dart';
part 'storage.g.dart';
@HiveType(typeId: 1)
class GlobalPosition {
GlobalPosition({required this.latitude, required this.longitude});
@HiveField(0, defaultValue: 1.0)
double latitude;
@HiveField(1, defaultValue: 1.0)
double longitude;
}
The error when load is :
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'GlobalPosition'
I don't know how to fix it, please help me, Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在
中使用
get 方法:You should use
defaultValue
inget
method: