0

I am using hive and getx together, I am trying to get all data inside hive with watch method:

   Stream<List<VocabularyModel>> watchVocabsFromdb() => hiveService.vocabularyBox
      .watch()
      .map((event) => hiveService.vocabularyBox.values
          .where((element) => element.remember == true)
          .toList());

In controller I am using this method:

class WordsController extends GetxController {
  WordsController();
  late Stream<List<VocabularyModel>> allVocabs;
  @override
  void onReady() {
    log.info('onReady');
    super.onReady();

    allVocabs = repository.watchVocabsFromdb();
  }

Inside view I am using Obx to catch change of hive box :

  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Obx(() => ListView.builder(
          itemCount: controller.allVocabs.length, ///... Future<int> is not int
              itemBuilder: (context, index) {
                return Container();
              },
            )),
      ),

How can I use hive whats like obs list?

zcoop98
  • 2,590
  • 1
  • 18
  • 31
Cyrus the Great
  • 5,145
  • 5
  • 68
  • 149

1 Answers1

0

Change late Stream<List<VocabularyModel>> allVocabs; to an RxList:

final allVocabs = RxList<VocabularyModel>([]);

Then on onInit() bind your stream to it:

allVocabs.bindStream(repository.watchVocabsFromdb());

After that you can observe it as usual using Obx or GetX.

S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30