I can´t persist data in Local storage in HiveBox, when i reload the page, everything in local database becomes null

1k views Asked by At

I'm newbie in dart/flutter. I need to get data of web API and put inside of Hive.box, as a LocalStorage. I can get an API data, and put in Hive, but I can't persist this data inside another pages, for example, I can't use the localStorage variable in another pages... The problem is the HiveBox is return null.

Main.dart - Open the box

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(),
  ));
}

RaisedButton in Login.dart

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() => in sessionValidation I put all data in 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();
  }
}

What return for me:

At first: 

I/flutter (14463): ------------------------------
I/flutter (14463): (3, Adm ConectCon, 53644, 4, 0, Condomínio SID, 509, 140, ..., null, 90377)
I/flutter (14463): --------------//---------------------
I/flutter (14463): (3, Adm ConectCon, 53644, 4, 0, Condomínio SID, 509, 140, ..., null, 90377)

After a hot reload: 

I/flutter (14463): --------------//---------------------
I/flutter (14463): ()
1

There are 1 answers

0
Yash Dixit On

I'm a little confused by the code, but here is what you can try:

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

You can make async, and in turn, you can await on your _abrirCaixa() method which is a future. This should solve the HiveBox is null error.