飞镖:不带运行功能的返回变量

我有一系列看起来像这样的物品:

  final List<Map<String, String>> dateIdeas = [
    {'Description': 'Drink Cocktails','Image': 'assets/images/cocktailsfull.jpg'},
    {'Description': 'Go for dinner!', 'Image': "assets/images/dinner.jpg"},
];

当用户点击按钮时,第一项将从数组中删除。

removeItem() {
    setState(() {
      dateIdeas.removeAt(index);
    });
  }

这可以正常工作。但是,我希望用户能够撤消此操作。我对此的解决方案是执行以下操作:

  1. Have the removeItem function return what the array looked like before the removal:
  Map<String, String> removeItemLiked() {
    final removed = dateIdeas[index];
    setState(() {
      _controller.reset();
      likes.add(dateIdeas[index]['Description']);
      dateIdeas.removeAt(index);
    });
    return removed;
  }
  1. Have an undo function that adds the item back into the array at index 0:
  void undo() {
    setState(() {
      Map<String, String> result = removeItemLiked();
      dateIdeas.insert(0, result);
    });
  }

The issue here is that when triggering the undo function it also retriggers removeItem. How can I have the old array stored to a variable so I can always use it later on?

评论
  • 小萝莉m
    小萝莉m 回复

    The removeItem is getting called by you within the undo function. To prevent this, separate the function call from the removed variable.

    // within stateful widget, declare 'removed'...
    Map<String, String> removed;
    

    您的其他函数将像这样被修改;

      void removeItemLiked() {
        removed = dateIdeas[index];
        setState(() {
          _controller.reset();
          likes.add(dateIdeas[index]['Description']);
          dateIdeas.removeAt(index);
        });
     }
    
      void undo() {
        if(removed != null) {
            setState(() {
              dateIdeas.insert(0, removed);
            });
        }
     }
    
  • Jim
    Jim 回复

    You can store the removed item in a variable like lastRemoved and store it's index and value both. Here's a simple example:

    class MyNums {
      List<int> numbers = [123, 34, 5, 436, 56, 677];
      Map<String, dynamic> lastRemoved;
    
      removeItem(int index) {
        lastRemoved = {
          'index': index,
          'item': numbers[index],
        };
        numbers.removeAt(index);
      }
    
      undoRemove() {
        if (lastRemoved != null) {
          numbers.insert(lastRemoved['index'], lastRemoved['item']);
        }
        lastRemoved = null;
      }
    }
    

    测试:

    main() {
      MyNums nums = MyNums();
      print('initial:\t\t ${nums.numbers}');
    
      nums.removeItem(2);
      print('remove from index 2:\t ${nums.numbers}');
    
      nums.undoRemove();
      print('undo index 2:\t\t ${nums.numbers}');
    
      nums.removeItem(2);
      print('remove from index 2:\t ${nums.numbers}');
    
      nums.removeItem(2);
      print('remove from index 2:\t ${nums.numbers}');
    
      nums.undoRemove();
      print('undo index 2:\t\t ${nums.numbers}');
    
      nums.undoRemove();
      print('nothing to undo:\t ${nums.numbers}');
    }
    

    输出:

    initial:                 [123, 34, 5, 436, 56, 677]
    remove from index 2:     [123, 34, 436, 56, 677]
    undo index 2:            [123, 34, 5, 436, 56, 677]
    remove from index 2:     [123, 34, 436, 56, 677]
    remove from index 2:     [123, 34, 56, 677]
    undo index 2:            [123, 34, 436, 56, 677]
    nothing to undo:         [123, 34, 436, 56, 677]