Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
511 views
in Technique[技术] by (71.8m points)

flutter - Updating index when deleting from a Box

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...