Can not retrieve data from Hive after application restart

Viewed 1340

Everything works fine until I restart the app (close the app and open it) and can not see previously saved data, but if I check the box status it is opened and empty. Am I doing something wrong?

  • Running on real device Xiaomi Redmi 4A (actually the problem appears also on Iphone devices)
  • Working on Mac M1
  • Flutter (Channel stable, 2.2.2, on macOS 11.2 20D64 darwin-arm, locale ru)
  • I found "problem" in github repo but there is nothing useful

main.dart

void main() async {
  // init hive
  WidgetsFlutterBinding.ensureInitialized();
  await lds.init();
  // init app
  runApp(MyApp());
}

hive init file

Future<void> init() async {
  final appDocumentDirectory = await path.getApplicationSupportDirectory();
  Hive.init(appDocumentDirectory.path);

  // registering entities
  Hive.registerAdapter(ItemModelAdapter());
}

save and load functions

class ItemLocalDataSource {
  static const String BOX_ITEMS = 'item-models';

  Future<List<ItemModel>> getLocalItems() async {
    await Hive.openBox(BOX_ITEMS);
    final box = Hive.box(BOX_ITEMS);
    final items = box.get(0) as List<ItemModel>;
    return items;
  }

  Future<void> setLocalItems(List<ItemModel> items) async {
    await Hive.openBox(BOX_ITEMS);
    final box = Hive.box(BOX_ITEMS);
    box.put(0, items);
    print('saved $items');
  }
}

package versions I am using in pubspec.yaml

  # hive (local data storage)
  hive: ^1.4.4+1
dev_dependencies:
  flutter_test:
    sdk: flutter
  # hive adapter generator
  hive_generator: ^0.8.2
  # build runner
  build_runner:
2 Answers

When I have upgraded Dart SDK , app have caused the same issue. When I have downgraded the Dart SDK, Hive started working as expected. Solution is downgrading Dart SDK. My current SDK is:

Dart SDK version: 2.14.2 (stable) 
Related