restoreStateInSharedPreferences method

Future<bool> restoreStateInSharedPreferences()

Restore the app saved state, usually called in the startup of the app.

Implementation

Future<bool> restoreStateInSharedPreferences() async {
  try {
    WidgetsFlutterBinding.ensureInitialized();
    SharedPreferences prefs = await SharedPreferences.getInstance();

    String result = prefs.getString('state') ?? '';
    if (encryptionKey != null && encryptionIV != null) {
      final _salsaKEY = encrypt.Key.fromUtf8(encryptionKey!);
      final _salsaIV = encrypt.IV.fromUtf8(encryptionIV!);

      /// Encrypter to protect saved states of the app and
      /// make hacking a lil bit harder.
      final _encrypter = encrypt.Encrypter(encrypt.Salsa20(_salsaKEY));

      final encrypted = encrypt.Encrypted.fromBase64(result);
      result = _encrypter.decrypt(encrypted, iv: _salsaIV);
    }

    if (result.isNotEmpty) this.setAppStateFromJson(result);

    return true;
  } catch (e) {
    return false;
  }
}