【发布时间】: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