在 Flutter 中使用 Hive 数据库时,我的应用程序冻结

问题描述

我在 main.dart 文件中初始化并打开 Hive 数据库框并执行 crud 操作。当我重新加载应用程序时应用程序冻结,但当我删除 Hive.openBox() 后面的“await”关键字时解冻; await Hive.openBox(ExpenseBoxName); 但应用将停止执行数据库操作。

enter image description here

Main.dart file where I initialize the database and opened the box

Where the data is supposed to be displayed

第三个片段是我执行 crud 操作的地方

解决方法

像这样初始化数据库

void main() async
{
  WidgetsFlutterBinding.ensureInitialized();
  final document = await getApplicationDocumentsDirectory();
  Hive.init(document.path);
  await Hive.openBox<String>(datbase_name");
  runApp(MyApp());
}