0

I am using hive and I am trying to listen to the value change of custom stored key as in the code below and I can't get any data ? So how can I achieve this ?

   Stream<BoxEvent> listenToLocalCartItem(String cartItemId) {
   var box = Hive.box('cart');
   box.watch(key: cartItemId).listen((event) {

    CartItemModel cartItem = event.value;
    if (cartItem.numberOfItems > 0) {
      emit(AddToCartState.showCartValue(cartItem.numberOfItems));
    } else {
      emit(AddToCartState.showAddButton());
    }
  });

}

On the other hand, I want to keep track of all data changes in the box as the code below like firestore snapshots and also I can't get any changes

 Stream<Box> listenToLocalCart() {
Hive.openBox('cart').asStream().listen((event) {
    _cartStatusProvider.cartItems = event.values.toList();
  });
 }
leftjoin
  • 36,950
  • 8
  • 57
  • 116
Omar Abdelazeem
  • 381
  • 2
  • 20

1 Answers1

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


FutureBuilder(
  future: Hive.openBox<CartItems>('cart'),
  builder: (context, snapshot) {
    return ValueListenableBuilder(
      valueListenable: Hive.box<CartItems>('cart').listenable(),
      builder: (context, Box<CartItems> box, _) {
        if (box.values.isEmpty) {
          return Text('data is empty');
        } else {
          return ListView.builder(
            itemCount: box.values.length,
            itemBuilder: (context, index) {
              var item = box.getAt(index);
              return ListTile(
                title: Text(item.name),
                subtitle: Text(item.count.toString()),
              );
            },
          );
        }
      },
    );
  },
),