hive icon indicating copy to clipboard operation
hive copied to clipboard

Hive storage not persistent on Flutter web

Open i-jared opened this issue 4 years ago • 4 comments

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

i-jared avatar Mar 04 '21 19:03 i-jared

Facing the same issue on version hive_flutter: ^1.0.0 hive: ^2.0.4

pauli2406 avatar May 21 '21 09:05 pauli2406

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");
}
}

wahabsohail avatar Oct 13 '21 13:10 wahabsohail

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

temirfe avatar Mar 17 '22 11:03 temirfe

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.

gotneb avatar May 23 '25 00:05 gotneb