Jonathon
Jonathon

Reputation: 1591

Listview won't update after sending back data (flutter)

I apologize for multiple questions as I'm new to Flutter.

I'm trying to send back data (list item index) from a route in flutter to remove from a ListView. However, the ListView is not updating. Not sure what I'm doing wrong here. I've cut some code short but if you need any more, let me know. I appreciate your help.

  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 Method:

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);
  }

Client screen

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

Upvotes: 0

Views: 266

Answers (1)

diegoveloper
diegoveloper

Reputation: 103421

The setState you are using is incorrect

Change this:

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

To this:

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

Upvotes: 5

Related Questions