我正在尝试在我的应用程序中使用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: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();
}
我对Hive里的一些事情不太确定:
当我使用 put()
方法是否保留计数器的值?
我正在初始化一个dart文件中的框,并在整个应用程序中将其作为全局变量调用,这会导致错误吗?
另外,我只添加了hive依赖项,因为我不需要其他依赖项。这也会引起问题吗?
我应该每次表演时打开盒子吗 get
& put
操作。
1条答案
按热度按时间elcex8rz1#
编辑
你可以初始化
Hive
box
在main
```Box box;
Future main() async{
WidgetsFlutterBinding.ensureInitialized();
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
box = await Hive.openBox('box');
runApp(MyApp());
}
void initHive() async {
await openBox();
getCounter();
}
void hiveOperation() async{
await _openBox();
updateInt();
}
@override
void initState() {
hiveOperation();
super.initState();
}
I/flutter ( 5675): 30
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
Box box;
Future 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 {
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: [
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),
),
);
}
}