【发布时间】:2022-11-28 01:08:33
【问题描述】:
我遇到了从 ValueListenableBuilder 内部不断重建的小部件子树的问题。它应该在更改时运行重建,在这种情况下,它正在监听 Flutter Hive 数据库上的表。
我累的事情:
- 我在 main 方法中打开了所有 Hive Box,这样我就可以从应用程序的任何位置访问每个 Box。我厌倦了只有在发生变化时才打开 Hive 框,然后立即关闭这个框。没用
我认为可能是但不确定的事情:
- 将 ChangeNotifierProvider 与 ValueListenableBuilder 混合 - 因为某些子树也使用 changenotifier,但是随着 ValueListenableBuilder 不断重建子树,我传递给提供者的任何更改都会被清除。
无论如何只能重建变化吗?
@override Widget build(BuildContext context) { return ValueListenableBuilder( valueListenable: Hive.box<Manifest>(HiveTables.manifestBox).listenable(), child: assignmentWidgets, builder: (context, Box<Manifest> manifestBox, child) { if (manifestBox.isNotEmpty) return child!; }, ); }
- 将 ChangeNotifierProvider 与 ValueListenableBuilder 混合 - 因为某些子树也使用 changenotifier,但是随着 ValueListenableBuilder 不断重建子树,我传递给提供者的任何更改都会被清除。
【问题讨论】:
标签: flutter dart flutter-provider flutter-hive