【问题标题】:Getx How to refresh list by using ObxGetx 如何使用 Obx 刷新列表
【发布时间】:2021-09-15 15:21:18
【问题描述】:

我正在使用 ReorderableSliverList,但我不知道如何根据我的数据动态观察列表。

屏幕 1

ReorderableSliverList(
          delegate: ReorderableSliverChildBuilderDelegate(
              (BuildContext context, int index) {
                final data = controller.products[index];
                    return ItemView(data);
            },
          childCount: controller.products.length),
          onReorder: _onReorder,
        ) 

在 screen2 将有一个添加按钮来调用控制器将新数据插入列表

控制器

var products = List<Product>.empty().obs;
void add(String name) {
    if (name != '') {
      final date = DateTime.now().toIso8601String();

      ProductProvider().postProduct(name, date).then((response) {
        final data = Product(
          id: response["name"],
          name: name,
          createdAt: date,
        );
        products.add(data);
        Get.back();
      });
    } else {
      dialogError("Semua input harus terisi");
    }
  }

上面的代码需要点击Hot reload,以便在屏幕2的数据发生变化时在屏幕1中显示数据。 我正在尝试使用 Obx 使其自动刷新,但结果仍然相同。

代码

ReorderableSliverList(
          delegate: ReorderableSliverChildBuilderDelegate(
              (BuildContext context, int index) {
            final data = controller.products[index];
            return Obx(
              () => controller.products.isEmpty
                  ? Center(
                      child: Text("BELUM ADA DATA"),
                    )
                  :  ItemView(data)        
            );
          }, childCount: controller.products.length),
          onReorder: _onReorder,
        )

【问题讨论】:

    标签: flutter flutter-sliver flutter-getx


    【解决方案1】:

    您需要像这样用 Obx 包装整个 ReorderableSliverList

    Obx(()=>ReorderableSliverList(
    ...
    ...
    ));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2022-11-04
      • 2021-11-15
      • 1970-01-01
      • 2022-01-02
      • 2022-10-17
      • 2021-11-23
      • 2021-11-07
      相关资源
      最近更新 更多