Flutter 我可以´;t将数据持久保存在HiveBox的本地存储中,当我重新加载页面时,本地数据库中的所有内容都将变为null

Flutter 我可以´;t将数据持久保存在HiveBox的本地存储中,当我重新加载页面时,本地数据库中的所有内容都将变为null,flutter,dart,persistence,flutter-hive,Flutter,Dart,Persistence,Flutter Hive,我是飞镖新手。我需要获取web API的数据并将其作为本地存储放在Hive.box中。我可以获取一个API数据,并将其放入配置单元中,但我不能将这些数据保存在其他页面中,例如,我不能在其他页面中使用localStorage变量。。。问题是HiveBox返回空值 Main.dart-打开盒子 Future _abrirCaixa() async { var dir = await getApplicationDocumentsDirectory(); Hive.init(dir.path)

我是飞镖新手。我需要获取web API的数据并将其作为本地存储放在Hive.box中。我可以获取一个API数据,并将其放入配置单元中,但我不能将这些数据保存在其他页面中,例如,我不能在其他页面中使用localStorage变量。。。问题是HiveBox返回空值

Main.dart-打开盒子

Future _abrirCaixa() async {
  var dir = await getApplicationDocumentsDirectory();
  Hive.init(dir.path);
  return await Hive.openBox('localStorage');
}

void main() {
  _abrirCaixa();
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyApp(),
  ));
}
Login.dart中的RaisedButton

Padding(
                padding: const EdgeInsets.only(top: 16.0),
                child: ButtonTheme(
                  height: 40.0,
                  child: RaisedButton(
                    onPressed: () async {
                      if (controladorUsuario.text.isEmpty ||
                          controladorSenha.text.isEmpty) {
                        camposVazios.camposVazios(context);
                      } else {
                        await fazerLogin(context);
                        infoAddADM();
                        infoAddCond();
                      }
                    },
                    // await infoAddCond();
                    // await infoAddADM();

                    child: Text(
                      "Conectar",
                      style: TextStyle(color: white),
                    ),
                    color: mainColor,
                  ),
                ),
              ),
fazerLogin()=>在sessionValidation中,我将所有数据放在LocalStorage中

[...]
 var dados = await Session.sessionValidation(login, senha);

    print('------------------------------');
    print(localStorage.values.toString());
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => TestePage(),
      ),
    );
[...]
TestPage.dart

class _TestePageState extends State<TestePage> {
  @override
  Widget build(BuildContext context) {
    print('--------------//---------------------');
    print(localStorage.values.toString());

    return Container();
  }
}

我对代码有点困惑,但以下是您可以尝试的:

void main() async{
  await _abrirCaixa();
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyApp(),
  ));
}

您可以将其设置为async,反过来,您也可以等待未来的_abircaixa()方法。这应该可以解决HiveBox为空的错误。

我对代码有点困惑,但以下是您可以尝试的:

void main() async{
  await _abrirCaixa();
  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    home: MyApp(),
  ));
}
您可以将其设置为async,反过来,您也可以等待未来的_abircaixa()方法。这将解决HiveBox为空的错误