发送回数据后,列表视图将不会更新(颤振)

时间:2019-07-11 23:45:20

标签: flutter dart

由于我是Flutter的新手,我为许多问题表示歉意。

我正试图从扑朔迷离的路由中发回数据(列表项索引),以从ListView中删除。但是,ListView不会更新。不知道我在做什么错。我缩短了一些代码,但是如果您需要更多代码,请告诉我。感谢您的帮助。

  List clients;

  Appointments({Key key, @required this.clients}): super(key: key);

  @override
  State<StatefulWidget> createState() {
    return AppointmentState();
  }
}

class AppointmentState extends State<Appointments> {
  @override
  Widget build (BuildContext context) {
    return Container(
      child: Expanded(
        child: ListView.builder(
          itemCount: widget.clients.length,
          itemBuilder: (context, index) {
            var client = widget.clients[index];

            return Dismissible(
              key: Key(client),
              direction:DismissDirection.endToStart,
              onDismissed: (direction) {
                setState () {
                  widget.clients.removeAt(index);
                }
              },
              background: Container(color: Colors.red),
              child: ListTile(
                title: Text(client),
                leading: Icon(Icons.face),
                trailing: Icon(Icons.keyboard_arrow_right),
                onTap: () {
                  //list index, and index data (client)
                  _checkIfRemoved(context, index, client);
                }
              ),
            );...

_checkIfRemoved方法:

void _checkIfRemoved (BuildContext context, index, client) async {
    final result = await Navigator.push(context, MaterialPageRoute(builder: (context) => Client(clientIndex: index, clientName: client)));
    setState() {
      widget.clients.removeAt(result);
    }
    print(result);
  }

客户端屏幕

RaisedButton(
              child: Text("Remove Client"),
              color: Colors.red,
              onPressed: () {
                Navigator.pop(context, widget.clientIndex);
              },
            )

1 个答案:

答案 0 :(得分:3)

您使用的setState不正确

更改此:

 setState() {
      widget.clients.removeAt(result);
    }

对此:

    setState(() {
       widget.clients.removeAt(result);
    });