4

I am writing a little code in flutter where I am using Hive. this is the code

    import 'package:flutter/material.dart';
    
    import 'package:path_provider/path_provider.dart';
    
    import 'package:hive_flutter/hive_flutter.dart';
    import 'posts.dart';
    
    
    late Box box;
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      final appDocumentDirectory = await getApplicationDocumentsDirectory();
      Hive.init(appDocumentDirectory.path);
      Hive.initFlutter();
      Hive.registerAdapter(PostsAdapter());
      print('adapter registered');
      box = await Hive.openBox<Posts>('posts');
      print('this is the box content ${box.values}');
      box.put('identifier:', Posts(identifier: '1', name: 'somename'));
      print(box.values);
      print(box.isEmpty);
      print('new values were added to the post');

      runApp(ExampleApp());
      }



    class ExampleApp extends StatelessWidget {
      const ExampleApp({Key? key}) : super(key: key);
    
      @override
    
      Widget build(BuildContext context) {
        Posts psts = box.get('posts');
    
        return MaterialApp(
            home: Scaffold(
              appBar: AppBar(
              title: Text("${psts.identifier}${psts.name}"),
            ),
        body: const Text('something to test'),
        ),);
      }
    }

the prints show what I would expect for results. the Box has an instance of Posts and with print(box.isEmpty) It is returning false. the error that I receive is the following:

The following _TypeError was thrown building ExampleApp(dirty):
type 'Null' is not a subtype of type 'Posts'

also this is the class Posts to complete the picture.

import 'package:hive/hive.dart';

part 'posts.g.dart';

@HiveType(typeId: 0)
class Posts extends HiveObject{

  @HiveField(0)
  late String? identifier;

  @HiveField(1)
  late String? name;
  Posts({required this.identifier, required this.name});

}

what am I doing wrong?

edit: I think I have narrowed down area of issue to the first Line in the build method. somehow the Posts psts = box.get('posts'); has something wrong with it.

leftjoin
  • 36,950
  • 8
  • 57
  • 116
Mufasa
  • 63
  • 5
  • 1
    Since this post, I have been now able to find a way and change the code to make 'something' out of it, I am still clueless on why the above code didn't work though, and I still don't see why is the box.get('posts') bugging, please if anyone could explain the issue and help me learn. – Mufasa Feb 27 '22 at 08:23
  • I had a similar problem when adding a new field to a TypeAdapter. I had it fixed by clearing flutter cache, rebuilding the generated Hive files and reinstalling the app on the emulator. – vladli Nov 12 '22 at 14:36

1 Answers1

1

I had a similar problem when adding a new field to a TypeAdapter. I had it fixed by:

  • clearing flutter cache flutter cache clean & flutter pub get
  • rebuilding the generated Hive files flutter packages pub run build_runner build
  • reinstalling the app on the emulator – delete the app, and it will be installed anew when you start flutter app
vladli
  • 1,454
  • 2
  • 16
  • 40