SliverList 和 Getx StateMixin 支持

SliverList and Getx StateMixin support

我正在努力尝试让 GetxControllerSliverList 一起工作。特别是,我的控制器 return 的视图状态为 StateMixin 来自 Getx library

class ItinerariesByCreatorPageController extends GetxController
    with StateMixin<Pair<Creator, List<Itinerary>>> {
  ItinerariesByCreatorPageController(this._interactor);

  final ItinerariesInteractor _interactor;

  void getCreatorPage(String creatorSlug, String language) {
    _interactor
        .getItinerariesByCreatorSlug(creatorSlug, language)
        .then((value) => change(value, status: RxStatus.success()))
        .onError<String>((error, stackTrace) =>
            change(null, status: RxStatus.error(error.toString())));
  }
}

在视图方面,我使用 obx 扩展来观察控制器状态,并使用 SliverList 来显示数据。不幸的是,SliverList 委托 SliverChildBuilderDelegate 需要在其构造函数中提供列表 childCount,但是 child 计数是从控制器异步而来的,我在 Getx 上找不到方法return child 的文档以反应方式计数。有没有办法让 SliverListGetx 一起工作?

Widget _getItinerariesList() {
    return SliverList(
        delegate: SliverChildBuilderDelegate(
            (context, index) => _controller.obx((data) {
                  if (index == 0) {
                    return CreatorInfoWidget(
                      creator: data.first,
                      imageWidget: CreatorImageWidget(
                        imageUrl: data.first.avatars.first,
                      ),
                    );
                  } else if (index == 1) {
                    return MainItineraryWidget(
                        creator: data.first, itinerary: data.second[index - 1]);
                  } else {
                    return SecondaryItineraryWidget(
                        creator: data.first, itinerary: data.second[index - 1]);
                  }
                }),
            childCount: 5 // use a dynamic child count here, coming from the controller));
  }

您可以在 SliverList

上方向上移动 controller.obx
Widget _getItinerariesList() {
return _controller.obx((data) {
  return SliverList(
        delegate: SliverChildBuilderDelegate(
        (context, index){
              if (index == 0) {
                return CreatorInfoWidget(
                  creator: data.first,
                  imageWidget: CreatorImageWidget(
                    imageUrl: data.first.avatars.first,
                  ),
                );
              } else if (index == 1) {
                return MainItineraryWidget(
                    creator: data.first, itinerary: data.second[index - 1]);
              } else {
                return SecondaryItineraryWidget(
                    creator: data.first, itinerary: data.second[index - 1]);
              }
            }),
        childCount: data.yourCountVariable // use a dynamic child count here, coming from the controller));
   }
}