Flutter:添加到 Hive 框的对象的字段在应用程序重启后返回 null

问题描述

我正在使用 Flutter 和 Hive 创建一个应用程序,但我还不熟悉它。
我需要在其中创建一个初始帐户,因此我为此创建了一个 Hive 框,并尝试在该框中保存一个帐户对象。
我检查添加打印并正确地将对象保存在框中。但是当我重新启动应用程序时,打印不再返回值。 Object 还在,但只有 String name 字段有值,其他两个是空值。
为什么重启后有些字段为空?

第一次运行后的输出

{"error": {
    "message": "(#100) param data must be an array.","type": "OAuthException","code": 100,"fbtrace_id": "Arug9LHzE4FKTY67LrqQZiJ"
  }
}

重启后输出

I/Flutter (14014): wallet
I/Flutter (14014): Currencies.USD
I/Flutter (14014): 0.0

主要代码

I/Flutter (14014): wallet
I/Flutter (14014): null
I/Flutter (14014): null

帐户类别代码

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  Directory document = await getApplicationDocumentsDirectory();

  Hive.registeradapter(CurrenciesAdapter());
  Hive.registeradapter(AccountAdapter());
  Hive.init(document.path);

  final accountBox = await Hive.openBox<Account>('accounts');

  if (accountBox.length == 0) { // default wallet
    Account wallet = new Account("wallet",Currencies.USD);
    accountBox.add(wallet);
  }

  print(accountBox.getAt(0).name.toString());
  print(accountBox.getAt(0).currency.toString());
  print(accountBox.getAt(0).cashAmount.toString());

  runApp(MyApp());
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)