Flutter Getx,在Obx错误中监听列表,怎么修?

q43xntqr  于 2023-06-24  发布在  Flutter
关注(0)|答案(1)|浏览(204)

我用Flutter中的Provider更新了listview中的列表。现在我正在尝试用Getx做同样的事情。我收到一个错误。我将在下面展示代码示例。我在网上找不到任何关于这件事的信息。如果你能帮忙我会很高兴的。首先,我向大家表示感谢。
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-这个方法和Provider一起工作得很好,我想在Getx用Obx包起来听它

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();
                    });
                  }
                },
              ),
            ));
      }
    });
  }
}
    • 请帮我解决这个问题。谢谢大家
o2gm4chl

o2gm4chl1#

尝试将小部件列表的创建移动到Obx内部,例如

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();
                    });
                  }
                },
              ),
            ));
      }
    });
  }
}

我相信这应该能解决你的问题

相关问题