walid thalib
walid thalib

Reputation: 329

how to call function in bloc pattern flutter

I have class required Function for delete named hapusCart.

I have bloc like this

Stream<List<KotakKeranjang>> mapEventToState(int event) async* {
    List<KotakKeranjang> kotakBanyak = [];
    for (int i = 0; i < event; i++)
      kotakBanyak.add(KotakKeranjang(
          nama: 'nama ' + i.toString(),
          price: ((i + 1) * 5000).toString(),
          jumlah: '3',
          hapuscart: () {}));
    yield kotakBanyak;
  }

and the output for listview builder

ListView.builder(
              itemCount: kotakKeranjang.length,
              itemBuilder: (context, index) {
                return Center(
                  child: Column(
                    children: [
                      KotakKeranjang(
                        nama: '${kotakKeranjang[index].nama}',
                        price: '${kotakKeranjang[index].price}',
                        jumlah: '${kotakKeranjang[index].jumlah}',
                        hapuscart: () {kotakkeranjang.removeAt(index);},
                      ),
                    ],
                  ),
                );
              },
            ),

I want to delete specific data in that list, I had try

kotakkeranjang.removeAt(index);

but it's not working. can anyone help?

Upvotes: 1

Views: 1219

Answers (1)

John Joe
John Joe

Reputation: 12803

You need to add setState to refresh the listView.

  setState(() {
      kotakkeranjang.removeAt(index);
  });

Upvotes: 1

Related Questions