hive icon indicating copy to clipboard operation
hive copied to clipboard

Flutter: Hive database is working well but when restarting the app the data is disappeared

Open MohammadSamiAbdelfattah opened this issue 4 years ago • 5 comments

This is Hive setup in main function that I open a box and also put the registerAdapter as I said in the question that everything is okay but when restarting the app data lost

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Hive.initFlutter();

  Hive.registerAdapter(QuestionModelAdapter());
  await Hive.openBox<QuestionModel>("saveQuestion");

  runApp(const MyApp());
}

This is the class I used in hive class.

import 'package:hive/hive.dart';

part 'question.g.dart';

@HiveType(typeId: 0)
class QuestionModel {
  @HiveField(0)
  String title;

  @HiveField(1)
  List<dynamic> options;

  QuestionModel({
    required this.title,
    required this.options,
  });
}

Version

  • Platform: Android
  • Flutter version: [2.0.0]
  • Hive version: [2.0.4]

MohammadSamiAbdelfattah avatar Nov 22 '21 22:11 MohammadSamiAbdelfattah

flutter desktop version also has this problem, any update? when reopen the app, the data is lost.

heliping avatar Dec 17 '21 03:12 heliping

Same issue here.

ousvat avatar Jan 24 '22 09:01 ousvat

same issue its not working well

vishwatejaaverager avatar Nov 28 '22 11:11 vishwatejaaverager

https://github.com/mohhamad-esmaili/Todo/blob/master/lib/controller/event_controller.dart

this two function solves void _eventBoxPutter() async { await _eventBox.put(_eventKey, _events); refreshItems(); }

/// this refresh items by getting again from box void refreshItems() { _events = _eventBox.get(_eventKey) ?? {}; _recycleEvents = _deletedBox.get(_deletedEventKey) ?? <Event>[]; selectedDay.refresh(); update(); }

ghost avatar Feb 16 '23 14:02 ghost

The same issue is happening also with simple String objects, where an adapter is not needed.

iJack93 avatar Jan 04 '24 10:01 iJack93