Flutter-GetX; Update list in item of list

Issue

I’m having a problem, I have a list let say (ListOne) and each item has a list let say (ListTwo), when I enter an item into ListOne, ListView.builder/ListView.separated for ListOne it works while when I enter an item into ListTwo ListView.builder/ListView.separated for ListTwo it doesn’t it works, I’ve tried using the Obx Widget but it doesn’t work

this is my LisView for ListOne

ListView.separated(
    itemBuilder: (context, index) {
         var _listOneItem = controller.listOne.elementAt(index);
         return ListOneItemWidget(item: __listOneItem);
    },
    separatorBuilder: (context, index) => SizedBox(height: 10.0),
    itemCount: controller.listOne.length,
),

and this is my ListView for ListTwo

ListView.builder(
    itemBuilder: (context, index) {
        var _listTwoItem = listOneItem.ListTwo.elementAt(index);
        return ListTwoItemWidget(item: _listTwoItem);
    },
    itemCount: listOneItem.ListTwo.length,
    shrinkWrap: true,
    physics: NeverScrollableScrollPhysics(),
),

Solution

if you are using GetX library then wrap the ListView.builder()/ListView.separated() with Obx() widget and try having the listOne and listTwo as Observable fields which you will be using in Obx() method. Make sure you use Observable fields in Obx() in the first level of its usage rather than using in the nested children widgets.

for Eg:
Proper Usage would be –

import 'package:get/get.dart';


List<dynamic> listOne = List.empty(growable : true).obs;
Obx(()
{
  var list = listOne.toList(growable:true);
  return ListView.builder();
}
);

Answered By – Mahendra Raj

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

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