![](/img/trans.png)
[英]How to pass a list from second screen to first screen in flutter?
[英]Flutter, how to update reactive list declared in first screen controller from the second screen?
我在我的項目中使用 GetX。 假設我有兩個屏幕。 每個屏幕都有一個按鈕。 第一個屏幕的 controller 有可觀察的對象列表。 當按下第一個屏幕中的按鈕時,object 列表中的一個被發送到第二個屏幕。
在第二個屏幕中,用戶更改了接收到的數據的值,當按下save
按鈕時,我也想更新第一個屏幕中的可觀察列表。
我知道我可以使用Get.back()
將更新的數據發送回第一個屏幕。 我可以在第二個屏幕中執行以下操作
Get.back(result: [
{"updatedObject": listDetails}
]);
在第一個屏幕上我可以做
Get.to(() => SecondScreen(), arguments: [
{"list": listDetails[index]}
]).then((result) {
// result[0]["updatedObject"] // use this to update list observable
});
我的問題。 按下“ Save
”按鈕時,我可以在不返回第一個屏幕的情況下更新第一個屏幕中的列表嗎?
我對 GetX 還很陌生,我正在努力學習它。 謝謝
只要當您使用 Get.to() 導航到第二個屏幕時,您就可以始終使用Get.to()
Get.find()
第一個屏幕的 controller 中的數據。
這是我稍微調整了一下的示例:
Get.to(() => SecondScreen(), arguments:
{"index": index},
)
在第二個屏幕上的 controller 上,您可以使用索引訪問(查看/更新)數據。
@override
void onInit() {
index = Get.arguments['index'];
// View
name = Get.find<FirstScreenController>().listDetails[index].name;
print(name);
// Update
Get.find<FirstScreenController>().listDetails[index].name = "John";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.