Flutter 颤振:蜂箱法';获取';方法调用null
我正在尝试在我的应用程序中使用Hive,以替代Flatter中的共享首选项。但是,我不断收到一个错误,上面写着:Flutter 颤振:蜂箱法';获取';方法调用null,flutter,flutter-hive,Flutter,Flutter Hive,我正在尝试在我的应用程序中使用Hive,以替代Flatter中的共享首选项。但是,我不断收到一个错误,上面写着: 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:
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.
我遵循了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 = await Hive.openBox('box');
return;
}
void getCounter() {
counter1 = box.get('counter1', defaultValue: 0);
// I am not storing any value initially, hence as it is null I want it
//to return the value 0 but the 'get' method keeps getting called on
//null.
}
void initState() {
initHive();
super.initState();
}
我不确定蜂巢中的一些事情:
- 当我使用
方法时,它是否会保留我的 柜台put()
- 我在一个dart文件中初始化该框,并将其称为 我的应用程序中的全局变量是否导致错误
- 另外,我只添加了配置单元依赖项,因为我不需要 休息这也会引起问题吗
- 每次执行
和get
操作时是否应打开该框put
- 编辑
您可以在
main
中初始化Hive
框
Box box;
Future<void> main() async{
WidgetsFlutterBinding.ensureInitialized();
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
box = await Hive.openBox('box');
runApp(MyApp());
}
您可以复制粘贴运行下面的完整代码步骤1:您需要
wait\u openBox()
并且要在initState()
中执行此操作,您可以使用函数hiveOperation()
执行async wait
代码片段
void initHive() async {
await openBox();
getCounter();
}
void hiveOperation() async{
await _openBox();
updateInt();
}
@override
void initState() {
hiveOperation();
super.initState();
}
步骤2:,您可以像使用地图一样使用蜂巢
。无需等待期货您不需要等待newBox.put('updateInt',updateInt)just
newBox.put('updateInt',updateInt)代码>将起作用
输出
I/flutter ( 5675): 30
完整代码
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
Box box;
Future<void> main() async{
WidgetsFlutterBinding.ensureInitialized();
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
box = await Hive.openBox('box');
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
int counter1;
void _incrementCounter() {
setState(() {
_counter++;
});
}
void getCounter() {
counter1 = box.get('counter1', defaultValue: 0);
_counter = box.get('counter1', defaultValue: 0);
print(counter1);
print(_counter);
// I am not storing any value initially, hence as it is null I want it
//to return the value 0 but the 'get' method keeps getting called on
//null.
}
@override
void initState() {
//initHive();
getCounter();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
导入“包装:颤振/材料.省道”;
导入“package:hive/hive.dart”;
导入“package:path_provider/path_provider.dart”;
盒子;
Future main()异步{
WidgetsFlutterBinding.ensureInitialized();
var dir=等待getApplicationDocumentsDirectory();
初始化(目录路径);
box=wait-Hive.openBox('box');
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
int计数器1;
void _incrementCounter(){
设置状态(){
_计数器++;
});
}
void getCounter(){
counter1=box.get('counter1',默认值:0);
_counter=box.get('counter1',defaultValue:0);
打印(计数器1);
打印(_计数器);
//我最初并没有存储任何值,因此,因为它为null,所以我需要它
//返回值0,但“get”方法一直被调用
//空。
}
@凌驾
void initState(){
//initHive();
getCounter();
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
我无法在示例代码中重现。请张贴您的完整代码,谢谢。嗨,我已经更新了我的问题,更准确,如果你能帮助我,我将不胜感激。谢谢我已经更新了我的答案。框为空,因为I/O需要时间。您可以在main中初始化配置单元框,请检查我的更新答案。这样您就不会遇到错误。我还更新了完整的代码。难道不能等到I/o完成后再更新吗?如果由于io仍在工作而绕过它,那么等待的意义何在。在我的例子中,我在initState中运行openBox,它在我的代码所在的Widget build()之后完成打开该框以访问配置单元框。导致相同的错误