Flutter:Hive 方法“get”方法在 null 上调用

Arn*_*nav 2 flutter flutter-hive

我试图在我的应用程序中使用 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 = 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();
}
Run Code Online (Sandbox Code Playgroud)

我不确定 Hive 中的一些事情:

  • 当我使用该put()方法时,它是否会保留我的计数器的值?
  • 我正在一个 dart 文件中初始化该框并将其作为整个应用程序中的全局变量调用是否会导致错误?
  • 另外,我只添加了 hive 依赖项,因为我不需要其余的。这也会导致问题吗?
  • 每次执行get&put操作时都应该打开盒子吗?

chu*_*han 5

编辑
您可以初始化Hive boxmain

Box box;

Future<void> main() async{
  WidgetsFlutterBinding.ensureInitialized();
  var dir = await getApplicationDocumentsDirectory();
  Hive.init(dir.path);
  box = await Hive.openBox('box');

  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

编辑
问题是代码执行顺序
为了让你更新的代码工作,你必须getCounter()输入initHive()

原因Hive box未准备好,因为I/O需要时间,执行getCounter() box时还没有准备好null
如果你遇到The method 'get/put' was called on null.那说明你box还没准备好
你必须再次检查async await和代码执行顺序
而且你不需要box每次都打开

代码片段

void initHive() async {
    await openBox();
    getCounter();
  } 
Run Code Online (Sandbox Code Playgroud)

您可以在
第 1 步下面复制粘贴运行完整代码:您需await _openBox()要这样做并且initState()可以使用一个函数hiveOperation()来执行async await
代码片段

 void hiveOperation() async{
    await _openBox();
    updateInt();
  }

  @override
  void initState() {
    hiveOperation();
    super.initState();
  }
Run Code Online (Sandbox Code Playgroud)

第 2 步:https : //pub.dev/packages/hive#usage,您可以Hive像使用map. 没有必要await Futures
你不需要await newBox.put('updateInt', updateInt);只是newBox.put('updateInt', updateInt);会工作

输出

I/flutter ( 5675): 30
Run Code Online (Sandbox Code Playgroud)

完整代码

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),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)