isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.11k stars 409 forks source link

listen not working with provider package #302

Open hsul4n opened 4 years ago

hsul4n commented 4 years ago

Steps to Reproduce listen not working with provider package.

Code sample

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

class SettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ValueListenableProvider.value(
      value: Hive.box('settings').listenable(),
      child: Consumer<Box>(
        builder: (context, box, widget) {
          return Switch(
              value: box.get('darkMode', defaultValue: false),
              onChanged: (val) {
                box.put('darkMode', val);
              });
        },
      ),
    );
  }
}

Version

ninest commented 4 years ago

Are you trying to create an app that rebuilds itself when the user switches from the light theme to dark theme?

hsul4n commented 4 years ago

Are you trying to create an app that rebuilds itself when the user switches from the light theme to dark theme?

I tried to use the example in the documentation using ValueListenableProvider instead of ValueListenerBuilder

DeepStyles commented 4 years ago

any updates on this one, used both ValueListenableProvider and ValueListenerBuilder but says listnable() not available.

error: the listenable() method isn't defined for type box.

hsul4n commented 4 years ago

any updates on this one, used both ValueListenableProvider and ValueListenerBuilder but says listnable() not available.

error: the listenable() method isn't defined for type box.

You need to open the box first so you can listen for changes, checkout example.

DeepStyles commented 4 years ago

ya its open before running app...

ex: await Hive.openBox('sessions'); runApp(Fomodoro() );

another error: await Hive.initFlutter(); (the method initFlutter isnt available for hive interface) this happened after updating hive and hiveflutter packages to latest....

hsul4n commented 4 years ago

You need to import hive_flutter.dart before you use await Hive.initFlutter(); because hive_flutter is an extenstion as shown at https://github.com/hivedb/hive/blob/d7ee72537a3b7ce1284bcbf5ee5f86e617a4f6fc/hive_flutter/lib/src/hive_extensions.dart#L4

I would suggest reading docs for more info.

DeepStyles commented 4 years ago

everything worked before, all imports does exist(checked out examples long time ago), only after packages update those errors showing. It says unused import so weird, just after updating everything goes collapsed.

DeepStyles commented 4 years ago

this solved issue for me...

https://github.com/hivedb/hive/issues/285#issuecomment-633815558

themisir commented 4 years ago

Hive.box('settings').listenable() emits BoxEvent stream, not Box. I think changing Consumer should work.

Consumer<BoxEvent>(
  ...
)
hsul4n commented 4 years ago

But the return type is Box as shown below https://github.com/hivedb/hive/blob/7e12deb179e99954c52d48c89801f19b5b7234df/hive_flutter/lib/src/box_extensions.dart#L10

themisir commented 4 years ago

But the return type is Box as shown below

https://github.com/hivedb/hive/blob/7e12deb179e99954c52d48c89801f19b5b7234df/hive_flutter/lib/src/box_extensions.dart#L10

Oh sorry, my mistake. 🤦‍♂️ I will investigate this issue tomorrow.

zachisit commented 3 years ago

Any updates to this @TheMisir ?

EdwynZN commented 3 years ago

I would recommend declaring the type, even if the box is dynamic so provider can find the correct box in the widget tree

ValueListenableProvider<Box<dynamic>>.value(
    value: Hive.box<dynamic>('settings').listenable(),
    child: Consumer<Box<dynamic>>(
      builder: (context, box, widget) {
        return Switch(
           value: box.get('darkMode', defaultValue: false),
           onChanged: (val)  => box.put('darkMode', val)
        );
      },
    ),
);
Scylla2020 commented 3 years ago

@EdwynZN I tried your recommendation and still got The method 'listenable' isn't defined for the type 'Box'.

baumths commented 3 years ago

@EdwynZN I tried your recommendation and still got The method 'listenable' isn't defined for the type 'Box'.

@Scylla2020

You have to add hive_flutter to your pubspec.yaml, the listenable() is an extension on Box that you can get from importing package:hive_flutter:hive_flutter.dart.