Want to remove indexed item in flutter's listview

Issue

There is an order list(contains orders) which is configured with pageview builder(Horizontal scroll) and in each order page there are items in listview.builder(vertical scroll), which I am able to successfully configure dynamically.

Now every order has n number of items, and each item has an button, which calls for action successfully. Now after the successful action, I want the order item in a order for which the action was executed should be removed from the listview.builder, because it gets removed in the server backend.

And when the order has no items left, it should be removed from the pageview.builder as well, because it is also removed from the server.

the code I am using is below for the widget of pageview.builder and list.viewbuilder

FutureBuilder(
            future: _future,
            builder: (context, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.none:
                  return Text('none');
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                case ConnectionState.active:
                  return Text('');
                case ConnectionState.done:
                  if (snapshot.hasError) {
                    return Text(
                      '${snapshot.error}',
                      style: TextStyle(color: Colors.red),
                    );
                  } else {
   return PageView.builder(
                        scrollDirection: Axis.horizontal,
                        itemCount: snapshot.data.content.length,// length of total orders

                        itemBuilder: (context, index) {
                            var firstdata = jsonResponse['content'];
                            var list = firstdata[index]['order_items'];
                          return Column(
                                          children:<Widget>[
                                    Text(  firstdata[index]['order_no]),
                                    ListView.builder(
                                  
                                  shrinkWrap: true,
                                  itemCount: //lenght of the items in the order to be determined,
                                  itemBuilder: (context, index) {
                                   return Column(
                                      children: [
                                         Text(list[index]['item_number']),
                                         RaisedButton(
                                          onPressed: (){
                                            callaction();
                                          },
                                        ) 

                                      ],
                                    );
                                  },
                                ),



                                        ])


                          
                        });
                  }
              }
            })

Function called

callaction(){
print('action called on server');


    var response = await http.post(url, body: data);
    if (response.statusCode == 200) {
     print('success');

    }
}

Please guide me on how should I achieve the desired functionality. json flutter indexing flutter-listview flutter-pageview

Solution

You could pass the index of the firstdata‘s item to callaction(). The problem is that the second builder’s index is shadowing the first, so you need to rename at least one of the two. Then you can do callaction(firstIndex) and from there, remove the correct item from firstdata.

Answered By – MickaelHrndz

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *