Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] #2

Closed
GroovinChip opened this issue Jul 23, 2019 · 6 comments
Closed

[Question] #2

GroovinChip opened this issue Jul 23, 2019 · 6 comments

Comments

@GroovinChip
Copy link

When creating a new box, like so: var box = await Hive.box('SettingsBox'); is box null until a value has been put into the box?

@simc
Copy link
Member

simc commented Jul 23, 2019

No it should be an instance of Box. There should also be a file settingsbox.hive created.

@GroovinChip
Copy link
Author

GroovinChip commented Jul 23, 2019

Odd. I'm messing around with using Hive on Windows, and I'm using Provider to send a box down the widget tree. I create it in the root widget like so:

class MyApp extends StatefulWidget {
  // This widget is the root of your application.
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var box;

  @override
  void initState() {
    makeBox();
    super.initState();
  }

  void makeBox() async {
    box = await Hive.box('SettingsBox');
  }

  @override
  Widget build(BuildContext context) {
    return Provider<Box>(
      builder: (context) => box,
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(),
      ),
    );
  }
}

When I access it in a different widget, like so: var box = Provider.of<Box>(context);, Flutter tells me that the runtimeType is null.

@simc
Copy link
Member

simc commented Jul 23, 2019

If I'm correct, the problem here is that makeBox() is not finished yet when build() tries to access the box. You don't await makeBox(). The problem is that in order to await makeBox(), initState() would need to be async which is not allowed.

I'm working on helpers for Flutter but until then you could use something like that:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FutureBuilder<Box>(
        future: Hive.box('SettingsBox'),
        builder: (BuildContext context, AsyncSnapshot<Box> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return SomePage();
          } else {
            return Center(
              child: Text('Please wait'),
            );
          }
        },
      ),
    );
  }
}

class SomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var box = Hive['SettingsBox'];
    // Do whatever you like with the box
    return Center(
      child: Text('Your App content'),
    );
  }
}

Btw: you don't have to pass boxes. If you already opened it, you can get the box instance using Hive['yourBoxName']

@GroovinChip
Copy link
Author

Okay, thank you very much!

@simc
Copy link
Member

simc commented Jul 23, 2019

I created a basic example if you are interested...

@GroovinChip
Copy link
Author

Thanks!

@cengbm cengbm mentioned this issue Feb 9, 2021
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants