0

Box not found. Did you forget to call Hive.openBox()? is the result of a call to Hive.openBox(). It is shown in the console. However the widget works fine and the contents of the box is shown correctly! I mean I know that the box is NOT open, that's why I open it...

Error message:

======== Exception caught by widgets library =======================================================
The following HiveError was thrown building FutureBuilder<Box<CreditCardOverview>>(dirty, state: _FutureBuilderState<Box<CreditCardOverview>>#d0a4f):
Box not found. Did you forget to call Hive.openBox()?

My flutter code:

// ...
Expanded(
 child: FutureBuilder(
          future: Hive.openBox<CreditCardOverview>('ccOverview'),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            // ... builder function checking snapshot etc.
          }
// ....

UPDATE This is the complete code of the state :

class _FinancialsListSmallState extends State<FinancialsListSmall> {
  @override
  Widget build(BuildContext context) {
    final sizeX = MediaQuery.of(context).size.width;
    final sizeY = MediaQuery.of(context).size.height - MediaQuery.of(context).viewInsets.bottom;

    return SafeArea(
      child: Container(
        width: sizeX,
        height: sizeY,
        child: Column(
          children: [
            PageTitleSmall(titleText: 'My Credit Cards', leadingIcon: Icon(Icons.credit_card)),
            Expanded(
              child: FutureBuilder(
                future: Hive.openBox<CreditCardOverview>('ccOverview'),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  List<Widget> children;
                  if (snapshot.hasData) {
                    children = <Widget>[
                      const Icon(
                        Icons.check_circle_outline,
                        color: Colors.green,
                        size: 60,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 16),
                        child: Text('Result: ${snapshot.data}'),
                      )
                    ];
                  } else if (snapshot.hasError) {
                    children = <Widget>[
                      const Icon(
                        Icons.error_outline,
                        color: Colors.red,
                        size: 60,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(top: 16),
                        child: Text('Error: ${snapshot.error}'),
                      )
                    ];
                  } else {
                    children = const <Widget>[
                      SizedBox(
                        child: CircularProgressIndicator(),
                        width: 60,
                        height: 60,
                      ),
                      Padding(
                        padding: EdgeInsets.only(top: 16),
                        child: Text('Awaiting result...'),
                      )
                    ];
                  }
                  return ListView(
                    children: showCreditCardOverview(),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

Any ideal what is going on here?

ThommyB
  • 1,456
  • 16
  • 34
  • maybe it is because of the `// ... builder function checking snapshot etc.` is doing something wrong? show your full code. – ch271828n Sep 11 '21 at 11:07
  • I updated the message. To me it looks like something is running out of order, but I don't see it. (something like a missing await() or so...) – ThommyB Sep 11 '21 at 12:53
  • 1
    see here: https://stackoverflow.com/questions/54594133/how-to-fix-the-error-the-following-assertion-was-thrown-building-futurebuilder You SHOULD return from your FutureBuilder. – Benyamin Sep 11 '21 at 16:55
  • @Benyamin Thanks, that hint helped me. I think the problem was a combination of things but I fixed it now. First I changed the first `if()` in the `FutureBuilder` and there return a private method which creates the ListView. The other `if statements` are just error handling, showing spinner etc. The creation of the ListView had also a `null safety` problem since the builder could return null, if the database is empty. After fixing all these things it works now. – ThommyB Sep 11 '21 at 20:10

1 Answers1

0

Open the box in main.dart instead of a particular file.

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

Void main()async{
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
Hive.registerAdapter(TAdapter());
await Hive.openBox<T>('boxName');
}

And now you can use this box in any file you want. For instance :

In example.dart

import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';

class _ExampleScreenState extends State<ExampleScreen>{
  Box<T> boxName = Hive.box<T>('boxName');

 @override
  Widget build(BuildContext context) {
//your code here
}
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arijeet
  • 935
  • 5
  • 19
  • That's a very bad advice since the entire data is loaded in memory when a box is opened as written [here](https://docs.hivedb.dev/#/advanced/lazy_box). Even with lazy loading all indexes are loaded into memory. If your boxes contain a bit of data its best to open when needed and to close the box in the `dispose()` method of the stateful widget to free memory. – ThommyB Sep 11 '21 at 12:47