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

Jon*_*hon 0 dart flutter

对于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);
                }
              ),
            );...
Run Code Online (Sandbox Code Playgroud)

_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);
  }
Run Code Online (Sandbox Code Playgroud)

客户画面

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

die*_*per 5

setState您使用的是不正确

更改此:

 setState() {
      widget.clients.removeAt(result);
    }
Run Code Online (Sandbox Code Playgroud)

对此:

    setState(() {
       widget.clients.removeAt(result);
    });
Run Code Online (Sandbox Code Playgroud)