flutterstateflutter-getxflutter-state

Flutter Getx. Listening to the list in Obx error. How to fix it?


I updated the list in listview with Provider in Flutter. Now I'm trying to do the same with Getx. I am getting an error. I will show the code example below. I couldn't find anything on the Internet about this. I would be very happy if you help. In advance, I express my thanks to everyone.

HomeController.dart

class HomeController extends GetxController {
  final RxList<Category> category = <Category>[].obs;
  final RxList<Post> slide = <Post>[].obs;
  final RxList<Post> posts = <Post>[].obs;

Future<void> getPost() async {
    await Service().getPost(start.value, limit).then((value) {
      if (value.isNotEmpty) {
        if (value.length < limit) {
          netStatus.value = 3;
          isLoading.value = false;
        } else {
          isLoading.value = true;
        }
        posts.addAll(value);
        start.value += limit;
      }
    });
  }

}

Post.dart - This Method works very well with Provider, I want to wrap it with Obx at Getx to listen to it

List<Widget> post(BuildContext context) {
  final CacheManager cacheManager = CacheService().cacheManager;
  HomeController controller = Get.find<HomeController>();

  List<Widget> posts = [
    ...controller.posts.map((post) => postCard(
          context,
          post,
          controller.category[int.parse(post.categoryId!) - 1],
          cacheManager,
        )),
  ];

  return posts;
}

Home.dart

class Home extends GetView<HomeController> {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    List<Widget> widgets = [
      Obx(() => slider()), //**Slider works with Obx very well**
      ...post(context), /***My problem starts here. I need to
 wrap it with Obx to listen to the contents of the list.
 I can wrap the slider widget, but I can't wrap the post.
 This is required for lazyloading in Listview. 
Otherwise, all the elements are loaded without going down.***/
    ];

    return Obx(() {
      if (controller.posts.isEmpty) {
        return loadHelper(context, controller.netStatus.value, () {
          controller.getCategory();
        });
      } else {
        return RefreshIndicator(
            edgeOffset: MediaQuery.of(context).padding.top,
            onRefresh: () => controller.onRefresh(),
            child: Obx(
              () => ListView.builder(
                shrinkWrap: true,
                controller: controller.scrollController,
                itemCount: widgets.length + 1,
                itemBuilder: (context, index) {
                  if (index < widgets.length) {
                    return Obx(() => widgets[index]);
                  } else {
                    return loadHelper(context, controller.netStatus.value, () {
                      controller.getSlide();
                    });
                  }
                },
              ),
            ));
      }
    });
  }
}

Please help me solve the problem. Thank you all.


Solution

  • Try moving the creation of the widget list to inside the Obx, so like

    class Home extends GetView<HomeController> {
      const Home({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Obx(() {
          List<Widget> widgets = [
            Obx(() => slider()),
            ...post(context)
          ];
          if (controller.posts.isEmpty) {
            return loadHelper(context, controller.netStatus.value, () {
              controller.getCategory();
            });
          } else {
            return RefreshIndicator(
                edgeOffset: MediaQuery.of(context).padding.top,
                onRefresh: () => controller.onRefresh(),
                child: Obx(
                  () => ListView.builder(
                    shrinkWrap: true,
                    controller: controller.scrollController,
                    itemCount: widgets.length + 1,
                    itemBuilder: (context, index) {
                      if (index < widgets.length) {
                        return Obx(() => widgets[index]);
                      } else {
                        return loadHelper(context, controller.netStatus.value, () {
                          controller.getSlide();
                        });
                      }
                    },
                  ),
                ));
          }
        });
      }
    }
    

    I believe that should solve your issue