小吃店中的撤消功能会引发有关无法安全访问停用的小部件的错误

时间:2019-01-10 15:20:45

标签: dart flutter

我的程序包装在包含用户数据的继承窗口小部件中。 稍后,我将创建一个脚手架,将数据显示为列表,然后从列表中删除元素。对于每次移除,我希望用户通过在小吃栏中提供取消按钮来取消移动,

但是,取消时我遇到以下问题:

══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (21713): The following assertion was thrown while handling a gesture:
I/flutter (21713): Looking up a deactivated widget's ancestor is unsafe.
I/flutter (21713): At this point the state of the widget's element tree is no longer stable. To safely refer to a
I/flutter (21713): widget's ancestor in its dispose() method, save a reference to the ancestor by calling
I/flutter (21713): inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.

使用UserDataContainer.of(context)访问用户数据无处不在,但仅在小吃店的操作中有效。例如,这是我可以解雇的图块:

return Dismissible(
  key: Key(UserDataContainer.of(context).widget.userdata[index].id.toString()), // Each Dismissible must contain a Key
  background: Container(
     color: Colors.red,
     child: Icon(Icons.delete),
     alignment: Alignment.centerLeft,
  ), // red background behind the item
  onDismissed: (direction) {
     // Each Dismissible must contain a Key
     var name_food = UserDataContainer.of(context).widget.userdata[index].name.toString();
     setState(() {
         removefromlist(index, context);
     });
     // Then show a snackbar!
     Scaffold.of(context).showSnackBar(SnackBar(
         content: Text("Item " + name_food + " dismissed"),
         action: new SnackBarAction(
              label: "UNDO",
              onPressed: (){
                   if(is_removed){
                   UserDataContainer.of(context).widget.userdata.add(last_removed); //THE OFFENDING LINE
                   is_removed = false;
              }
          },
       ),
     ));
  },
  child: _buildRow(UserDataContainer.of(context).widget.userdata[index],index),
);

在小吃店中调用函数实际上试图告诉我时,是什么错误?

0 个答案:

没有答案