Dismissing a Dismissible with Flutter/Dart

后端 未结 5 1171
轻奢々
轻奢々 2021-01-03 17:53

In the majority of the Dismissible examples provided by Flutter, they are dismissing items within a ListView. For example, this.

What I am currently doing is this:

相关标签:
5条回答
  • 2021-01-03 18:32

    simplest way 1-> set unique id for each item of list with

    var uuid = new Uuid();
    
      new MyItem(title: "Sanjay Singh Bisht",color:"#123ab",uniqueId:uuid.v1()));
    

    as mentioned in above post Dismissible widget required unique id

    2-> now to delete item its simple

    if (items.contains(deletedItem)) {
        setState(() {
          items.remove(deletedItem);
        });
      }
    

    3- to undo delete item just update that item id so that Dismissible widget has unique id always

    setState(() {
    deletedItem.uniqueId=uuid.v1();
    });
    
    0 讨论(0)
  • 2021-01-03 18:32

    The error message is pretty clear.

    Make sure to implement the onDismissed handler and to immediately remove the Dismissible widget from the application once that handler has fired.

    An empty function is not enough. Once an item has been dismissed, that widget has to be removed from the widget tree. Which means that you must remove the Dismissible from your ListView.

    0 讨论(0)
  • 2021-01-03 18:36

    The error comes when the widget is dismissed but not removed from the tree since the state still contains the dismissed object. Ideal implementation for onDismissed should remove the item and set the new state

    So in your example you would do something like this

    onDismissed: (DismissDirection direction) { dismissPerson(person); }
    

    and in dismissPerson function remove the person and set new state.

    dismissPerson(person) {
        if (_personList.contains(person)) {
        //_personList is list of person shown in ListView
          setState(() {
            _personList.remove(person);
          });
        }
    }
    

    If you refer to the same link posted in the question it now contains proper implementation for dismissible. Adding the relevant snippet of code from the link for convenience

    final Widget card = new Dismissible(
          key: new ObjectKey(cardModel),
          direction: _dismissDirection,
          onDismissed: (DismissDirection direction) { dismissCard(cardModel); },
    
          ....
    
        );
    
    
    void dismissCard(CardModel card) {
        if (_cardModels.contains(card)) {
          setState(() {
            _cardModels.remove(card);
          });
        }
    }
    
    0 讨论(0)
  • 2021-01-03 18:37

    Make sure the values you're passing to the key parameters are unique as well. And DO NOT use the index of an item. Since after removing an item from an array, the array will shift the positions of the items, the Dismissable widget will not recognize the removal of an item.

    0 讨论(0)
  • 2021-01-03 18:50

    Please try this.I provided UniqueKey as key to Dismissible widget, and it worked just fine.

    key: UniqueKey(), 
    
    0 讨论(0)
提交回复
热议问题