Chris
Chris

Reputation: 175

List [index +1] works [index - 1] doesn't (Flutter)

I have a method to skip to the next list item, which works fine, but the method to display the previous list item doesn't seem to re-render.

When I print to console, the list item index it goes down by 1, but the text widget doesn't update like it does when it increases by 1.

I have shown the 2x methods below and an excerpt from the build. Help! :)

void _skipFlashcard () {
    setState(() {
      int currentIndex = allFlashcards.indexOf(widget.flashcardToShow);
      var nextFlashcard = allFlashcards[currentIndex + 1];
      widget.flashcardToShow = nextFlashcard;
      print(widget.flashcardToShow.flashcardId);
    });
  }

  void _previousFlashcard () {
    int currentIndex = allFlashcards.indexOf(widget.flashcardToShow);
    var previousFlashcard = allFlashcards[currentIndex - 1];
    widget.flashcardToShow = previousFlashcard;
    print(widget.flashcardToShow.flashcardId);
  }

-------------------------
          Container(
            child: Row(
              children: <Widget>[
                Text(widget.flashcardToShow.flashcardId.toString()),

Upvotes: 0

Views: 1136

Answers (1)

Tom Alabaster
Tom Alabaster

Reputation: 985

Wrap your code in setState, that's all that is missed :-)

void _previousFlashcard () {
  setState() {
    int currentIndex = allFlashcards.indexOf(widget.flashcardToShow);
    var previousFlashcard = allFlashcards[currentIndex - 1];
    widget.flashcardToShow = previousFlashcard;
    print(widget.flashcardToShow.flashcardId);
  }
}

Upvotes: 1

Related Questions