isar / isar/hive

integrating Hive database with Riverpod

Open
#758 0 comments 1 reaction 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

There is very easy way to use `Hive` key-value database on `StatefulWidgets`, for example:

```dart
class HookDemo extends StatefulWidget {
@override
_HookDemoState createState() => _HookDemoState();
}

class _HookDemoState extends State {
Box user;
@override
void initState() {
super.initState();

user = Hive.box('user');
}

@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {
final _u = User()
..nameFamily = 'myname'
..mobileNumber = '123456789';
_user!.add(_u);
_u.save();
},
child: Icon(Icons.add),
),
...
);
}
}
```
here we defined `Box user` property and inside `initState` we implemented what's user such as `user = Hive.box('user');`

after that we can use `user` without any problem and getting `already opened` error

now in this current application we used `HookWidget` and when we want to use `Hive` we get error as `box already opened`

`main.dart`:

```dart
Future initHiveDriver() async {
final appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
await Hive.initFlutter(appDocumentDirectory.path);

await Hive.openBox('user');
}

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
///...
initHiveDriver();

runApp(
ProviderScope(observers: [
Logger()
],
child: MyApp()),
);
}
```

how can i create a provider for `Hive` with `Riverpod` and use it into `HookWidget`?

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with main.dart, especially initHiveDriver and main, then trace how the HookWidget is expected to obtain Hive access through Riverpod. A complete result should document a provider setup that lets the HookWidget use the user box without the "box already opened" error; no test file is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
databases, mobile-dev
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.