I have a FutureBuilder
that has a child of ValueListenableBuilder
that contains a
valueListenable: Hive.box<Note>('noteBox').listenable(),
builder: (context, Box<Note> notes, _) {
itemBuilder: (context, index, animation) {
var note = Hive.box<Note>('noteBox').getAt(index);
}
When I use setState(() {Hive.box<Note>('noteBox').deleteAt(id)})
in another file, I get an error saying
RangeError (index): Index out of range: index should be less than 6: 6
which I know what it means but don't get why I get it. Shouldn't the index update when the length of the items in the Box change?
Here is the full widget tree
future: _engageWithDb(),
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
),
);
}
if (snapshot.hasError) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.red,
),
);
} else {
return ValueListenableBuilder(
valueListenable: _noteBox.listenable(),
builder: (context, Box<Note> notes, _) {
if (notes.values.isEmpty) {
return Center(
child: Text(
'No notes',
style: TextStyle(
color: Colors.grey,
),
),
);
}
return AnimatedList(
initialItemCount: notes.length,
itemBuilder: (context, index, animation) {
var note = notes.getAt(index);
print(index);
return NoteBox(
passedId: index,
passedTitle: note.title != null || note.body != ''
? note.title
: 'Untitled note',
passedBody: note.body != null || note.body != ''
? note.body
: 'Such empty',
);
},
);
},
);
}
},
);
And in another I change the values in the box with
() async {
await Hive.openBox<Note>('noteBox');
var _noteBox = Hive.box<Note>('noteBox');
setState(() {
_noteBox.deleteAt(widget.passedId);
});
},
question from:
https://stackoverflow.com/questions/65874696/updating-index-when-deleting-from-a-box 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…