Can not retrieve data from Hive after application restart

689

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) 
Share:
689
1encore
Author by

1encore

20 y.o. Full-stack dev

Updated on December 30, 2022

Comments

  • 1encore
    1encore over 1 year

    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: