列表[index +1]有效[index-1]无效(颤振)

时间:2019-03-21 21:28:45

标签: list flutter setstate

我有一个方法可以跳到下一个列表项,效果很好,但是显示前一个列表项的方法似乎没有重新呈现。

当我打印到控制台时,列表项索引下降1,但是文本小部件不会像增加1时那样更新。

我已经在下面显示了2x方法和构建摘录。救命! :)

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()),

1 个答案:

答案 0 :(得分:1)

将您的代码包装在setState中,这就是所有要错过的事情:-)

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