can't refresh listview after update and delete flutter

Viewed 378

Hello I'm a new flutter user, here I'm trying to complete CRUD but when I finish adding, editing, and deleting the listview page it can't be refreshed immediately. When I re-open the new page can be refreshed. Do you think friends here can solve my problem? Thank you good friends can help me. Here's my source code snippet

Here is my source code snippet :

listnasabah.dart

class ListNasabah {
  ApiService apiService;
  ListNasabah({this.apiService});

  void getNasabah() {
    apiService.getNasabah().then((nasabah) {
      print(nasabah);
    });
  }

  Widget createViewList() {
    return SafeArea(
      child: FutureBuilder(
        future: apiService.getNasabah(),
        builder: (BuildContext context, AsyncSnapshot<List<Nasabah>> snapshot) {
          if (snapshot.hasError) {
            return Center(
              child: Text(
                'Something wrong with message: ${snapshot.error.toString()}',
                textAlign: TextAlign.center,
              ),
            );
          } else if (snapshot.connectionState == ConnectionState.done) {
            List<Nasabah> nasabah = snapshot.data;
            return nasabahListView(nasabah);
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }

  Widget nasabahListView(List<Nasabah> listnasabah) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
      child: ListView.builder(
        shrinkWrap: true,
        itemBuilder: (context, index) {
          Nasabah nasabah = listnasabah[index];
          return Padding(
            padding: const EdgeInsets.only(top: 8.0),
            child: Card(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text(
                      nasabah.nama_debitur,
                      style: Theme.of(context).textTheme.bodyText1,
                    ),
                    Text(nasabah.alamat),
                    Text(nasabah.no_ktp),
                    Text(nasabah.no_telp),
                    Text(nasabah.no_selular),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.end,
                      children: <Widget>[
                        FlatButton(
                          onPressed: () {
                            apiService
                                .deleteNasabah(nasabah.id)
                                .whenComplete(() {
                              Alert(
                                context: context,
                                type: AlertType.success,
                                title: "Sukses",
                                desc: "Data berhasil dihapus",
                                buttons: [
                                  DialogButton(
                                    child: Text(
                                      "OK",
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 20),
                                    ),
                                    onPressed: () {
                                      Navigator.pop(context);
                                    },
                                    width: 120,
                                  )
                                ],
                              ).show();
                            });
                          },
                          child: Text(
                            "Hapus",
                            style: TextStyle(color: Colors.red),
                          ),
                        ),
                        TextButton(
                          onPressed: () async {
                            var result = await Navigator.push(context,
                                MaterialPageRoute(builder: (context) {
                                  return FormAddNasabah(nasabah: nasabah);
                                }));
                          },
                          child: Text(
                            'Edit',
                            style: TextStyle(color: Colors.blue),
                          ),
                        ),
                      ],
                    )
                  ],
                ),
              ),
            ),
          );
        },
        itemCount: listnasabah.length,
      ),
    );
  }
}

datanasabah.dart

class DataNasabah extends StatefulWidget {
  DataNasabah({Key key}) : super(key: key);
  final String title = "Data Nasabah";

  @override
  _DataNasabahState createState() => _DataNasabahState();
}

class _DataNasabahState extends State<DataNasabah> {
  ApiService apiService;
  ListNasabah _listNasabah;

  @override
  void initState() {
    super.initState();

    apiService = ApiService();
    _listNasabah = new ListNasabah(apiService: apiService);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldState,
      appBar: AppBar(
        title: Text(
          'Data Nasabah',
          style: TextStyle(color: Colors.white),
        ),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => FormAddNasabah(),
                ),
              );
            },
            child: Padding(
              padding: const EdgeInsets.only(right: 16.0),
              child: Icon(
                Icons.add,
                color: Colors.white,
              ),
            ),
          )
        ],
      ),
      body: _listNasabah.createViewList(),
    );
  }
}
1 Answers

If it's getting refreshed after you come back from another page then its problem with rendering UI as you are not using any state management here. You can check state management in flutter, I would suggest to use Provider(https://pub.dev/packages/provider).

For stateful widget you can use setState function.

Related