GetX UI state not changing on ListTile

Issue

I have a list of objects, but I want to change the state of one object to "isLoading" where it will have a different title, etc.

I’m building my list view:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        key: scaffoldKey,
        body: Obx(() => buildListView(context)));
  }

  Widget buildListView(BuildContext context) {
    return ListView.builder(
        itemCount: controller.saveGames.length,
        itemBuilder: (context, index) {
          final saveGame = controller.saveGames.elementAt(index);

          return saveGame.isLoading
              ? buildListTileIsLoading(context, saveGame)
              : buildListTile(context, saveGame);
        });
  }

  ListTile buildListTile(BuildContext context, SaveGame saveGame) {
    return ListTile(
      onTap: () => controller.process(saveGame)
    );
  }

The controller:

class SaveGameController extends GetxController {

  final RxList<SaveGame> saveGames = <SaveGame>[].obs;
  
  void process(SaveGame saveGame) {
    saveGame.working = true;

    update();
  }
  
}

Where have I gone wrong here?

edits: Added more code

Solution

So despite the fact, I’m only updating one object in the list and not modifying the content of the list (adding/removing objects) I still need to call saveGames.refresh();

An oversight on my end didn’t think you’d need to refresh the entire list if you’re just changing the property on one of the objects.

Good to know 🙂

Answered By – Chris Turner

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published