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
Are you trying to create an app that rebuilds itself when the user switches from the light theme to dark theme?
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
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.
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.
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....
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.
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.
this solved issue for me...
https://github.com/hivedb/hive/issues/285#issuecomment-633815558
Hive.box('settings').listenable() emits BoxEvent stream, not Box. I think changing Consumer should work.
Consumer<BoxEvent>(
...
)
But the return type is Box as shown below https://github.com/hivedb/hive/blob/7e12deb179e99954c52d48c89801f19b5b7234df/hive_flutter/lib/src/box_extensions.dart#L10
But the return type is
Boxas shown below
Oh sorry, my mistake. 🤦♂️ I will investigate this issue tomorrow.
Any updates to this @TheMisir ?
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)
);
},
),
);
Most helpful comment
Oh sorry, my mistake. 🤦♂️ I will investigate this issue tomorrow.