Hive storage not persistent on Flutter web
Steps to reproduce I am using hive with flutter web, and after closing the project and starting it up again the stored data comes back as null. After storing data with hive, stop flutter, close the browser, and start it up again.
Code sample
class _MyHomePageState extends State<MyHomePage> {
int counter;
bool loading = true;
var box;
@override
void initState() {
init();
super.initState();
}
void init() async {
await Hive.initFlutter();
var getBox = await Hive.openBox<int>('testBox');
int oldval = getBox.get('counter');
setState(() {
box = getBox;
counter = oldval ?? 0;
loading = false;
});
}
@override
void dispose() {
box.close();
super.dispose();
}
void increment() async {
setState(() => counter += 1);
await box.put('counter', counter);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: loading ? CircularProgressIndicator() : Text('$counter'),
),
floatingActionButton: FloatingActionButton(
onPressed: increment,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
Version
- Platform: Windows, Web
- Flutter version: 1.26.0-17.5.pre (beta channel)
- Hive version: 1.4.4
- Hive_flutter version: 0.3.1
Facing the same issue on version hive_flutter: ^1.0.0 hive: ^2.0.4
I am facing the same issue on version hive_flutter: ^1.1.0 hive: ^2.0.4 my code
Future addTransaction(
{String userName,
int userId,
String userToken,
String userFullName,
String userProfileImage,
String userFirstName,
String userLastName}) async {
try {
final transaction = MultiAccount()
..userId = userId
..userName = userName
..userFirstName = userFirstName
..userLastName = userLastName
..userFullName = userFullName
..userToken = userToken
..userProfileImage = userProfileImage;
final box = Boxes.getTransactions();
final transactions = box.values.toList().cast<MultiAccount>();
bool res = checkDataExist(transactions, userId);
if (res == false) {
box.add(transaction);
}
} on Exception catch (e) {
// TODO
print("add error hive ... $e");
}
}
In debug mode port is random on each run and storage is fresh. To make it persistent do flutter run -d chrome --web-port 5555 as in this answer
It didn't work for me. It seems Flutter uses a incognito mode that even at a selected port the data is not persisted. My solution was flutter run -d web-server --web-port 5555 and in any browser tab going to http://localhost:5555.