ValueListenableBuilder not rebuilding UI when new items added to Box
- Dominant language
- Dart
- Stars
- 4.4k
- Forks
- 449
- PR merge metrics
- No merged PRs in 30d
Description
**Question**
I'm trying to implement a very basic prototype of showing updated Box entries in the UI using the `ValueListenableBuilder`, but new entries are simply not showing. I'm seeing conflicting examples of how to show such logic (either by opening the box in a `FutureBuilder` or trying to `openBox(name)` in the `main()` method of the application, or older examples using the deprecated `WatchBuilder`) so I'm getting lost here and spending time trying multiple directions with no result.
**Code sample**
This is my main list view:
```dart
import 'package:app_name_flutter/common_widgets/error_message_full.dart';
import 'package:app_name_flutter/models/battery_information.dart';
import 'package:app_name_flutter/views/services_list/storage/battery_storage_service_detail.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
class BatteryStorageList extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
key: UniqueKey(),
future: Hive.openBox('batteryServiceBox'),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.hasError) {
return ErrorMessageFull(
message: snapshot.error.toString(),
);
}
return BatteryStorageServiceDetail();
}
return const Scaffold();
},
),
);
}
}
```
This is the implementation view:
```dart
import 'package:app_name_flutter/common_widgets/app_bar_white.dart';
import 'package:app_name_flutter/models/battery_information.dart';
import 'package:flutter/material.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
class BatteryStorageServiceDetail extends StatefulWidget {
@override
_BatteryStorageServiceDetailState createState() =>
_BatteryStorageServiceDetailState();
}
class _BatteryStorageServiceDetailState extends State {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBarWhite(
title: 'Storage Detail',
),
body: Column(
children: [
const SizedBox(
height: 20,
),
Expanded(
child: ValueListenableBuilder(
valueListenable: Hive.box('batteryServiceBox')
.listenable(),
builder: (context, Box _batteryBox, _) {
return ListView.builder(
itemCount: _batteryBox.values.length,
itemBuilder: (BuildContext context, int index) {
final battery = _batteryBox.getAt(index);
return ListTile(
title: Text(
battery.batteryLevel.toString(),
),
);
},
);
},
),
),
],
),
);
}
}
```
I can see the entries on the first page load, but it never updates the UI.
Intermittently I'm seeing errors such as "_The Box is already open and of type Box_". I do have other helper classes actively saving to this Box adding entries every second.
**Version**
- Platform: iOS, Android
- Flutter version: 1.23.0-18.1.pre • channel beta
- Hive Version: hive: ^1.4.4+1, hive_flutter: ^0.3.1
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.